manytomanyfield

Way to allow for duplicate many-to-many entries in Python/Django

ぐ巨炮叔叔 提交于 2019-12-18 09:04:16
问题 I have the following Django model: class Icon(models.Model): name = models.CharField(max_length=200,null=False,blank=False) class Post(models.Model): icons = models.ManyToManyField(Icon) When I write the following code: post = Post() icons = [] icon_id = form.cleaned_data['icon_1'] if (icon_id): i = Icon.objects.get(id=icon_id) icons.append(i) icon_id = form.cleaned_data['icon_2'] if (icon_id): i = Icon.objects.get(id=icon_id) icons.append(i) post.icons = icons post.save() It works fine for

Django: ManyToMany filter matching on ALL items in a list

梦想的初衷 提交于 2019-12-17 09:49:53
问题 I have such a Book model: class Book(models.Model): authors = models.ManyToManyField(Author, ...) ... In short: I'd like to retrieve the books whose authors are strictly equal to a given set of authors. I'm not sure if there is a single query that does it, but any suggestions will be helpful. In long: Here is what I tried, (that failed to run getting an AttributeError) # A sample set of authors target_authors = set((author_1, author_2)) # To reduce the search space, # first retrieve those

Django - Migrate ManyToMany to through

人走茶凉 提交于 2019-12-14 04:18:00
问题 I have a model that looks like this: class Assignment(models.Model): """An assignment covers a range of years, has multiple coders and one specific task""" title = models.CharField(blank=True, max_length=100) start_date = models.DateField(default=date.today) end_date = models.DateField(default=date.today) coders = models.ManyToManyField(User, related_name='assignments') I want to change this model (which has data already in production) so that it looks like this: class Assignment(models.Model

Django saving to ManyToMany fields

走远了吗. 提交于 2019-12-13 20:34:05
问题 I have a simple model class with 2 ManyToManyField fields like this: models.py class Folder(models.Model): user = models.ManyToManyField(User) asset = models.ManyToManyField(Asset) In my view, I know the user ID and the asset ID. Say the user ID is 1 and the asset ID is 30, how do I inject this row? I guess I don't understand how to instantiate Folder so I can save/update the row. views.py def addAssetToMyFolder(request, id=None): ''' view is simplified for brevity ''' f = Folder( user = 1,

Django Model ManyToMany Reverse Filter

白昼怎懂夜的黑 提交于 2019-12-13 02:25:42
问题 Here's excerpts from (something analogous to) my models: class Person(models.Model): name = models.CharField(max_length=20) relationships = models.ManyToManyField('self', through='Relationship', symmetrical=False, related_name='related_to', ) def __str__(self): return self.name class Relationship(models.Model): from_person = models.ForeignKey(Person, related_name='from_people', on_delete=models.CASCADE, ) to_person = models.ForeignKey(Person, related_name='to_people', on_delete=models.CASCADE

Django ManyToMany filter with more than one condition

被刻印的时光 ゝ 提交于 2019-12-12 01:55:53
问题 My simplified models are as follows: class Function(models.Model): name = models.CharField(max_length=20) params = models.ManyToManyField("Param") class Param(models.Model): name = models.CharField(max_length=20) value = models.CharField(max_length=20) So, every function object has the set of parameters, for example: f = Function(name="my_function") f.save() param1 = Param(name="height", value="100") param1.save() param2 = Param(name="width", value="200") param2.save() f.params.add(param1) f

How can I add the same object to a ManyToMany field?

痴心易碎 提交于 2019-12-11 12:16:01
问题 I need help on how to save the same (reference to an) object into a ManyToManyField. For example I have models like this: class Material(models.Model): name = models.CharField(max_length=50) class Compound(models.Model): materials = models.ManyToManyField(Material) In this example, the Compound can be made of one or many different Material s, and it also could be made from the same Material twice (same id in Material model). If I try to save through a ModelForm , the second Material is

Querying django ManyToMany

孤街醉人 提交于 2019-12-11 07:57:55
问题 I have got Foo <=> FooGroup <=> Bar relation, where <=> stands for ManyToMany field. How do I retrieve all the Foo s for a specific Bar instance? 回答1: Here's an example with auth models, where the relationship is very much like your structure : User <=> Groups <=> Permission from django.contrib.auth import models models.Permission.objects.filter(group__user=models.User.objects.get(username="webmaster")) With your example: Foo.objects.filter(foogroup__bar=barinstance) 来源: https://stackoverflow

My Django manytomany fields are all marked unique, is there an option to remove this?

最后都变了- 提交于 2019-12-11 01:47:27
问题 Given a model like this: class A(models.Model): def __unicode__(self): return "%d"%self.id class B(models.Model): a_set = models.ManyToManyField(A) def __unicode__(self): return "%d"%self.id Then the following series of operations demonstrates my problem: In [1]: a1=A() In [2]: a1.save() In [3]: a1 Out[3]: <A: 1> In [4]: b1=B() In [5]: b1.save() In [6]: b1 Out[6]: <B: 1> In [7]: b2=B() In [8]: b2.save() In [9]: b2 Out[9]: <B: 2> In [10]: a1.b_set.add(b1) In [11]: a1.b_set.all() Out[11]: [<B:

Question on Django: Displaying many to many fields

蹲街弑〆低调 提交于 2019-12-11 01:37:19
问题 I seem to have a problem with Django when it comes Rendering ManyToManyField in a template. I can make it work partially, but I cannot make it work properly as I want it. Firstly I have an invoice template which displays Invoice details from my data base #invoice_details.html {% extends "base.html" %} {% block content %} <h2>Invoice Details</h2> <div id="horizontalnav"> <a href="/index/add_invoice">Add an Invoice</a> <a href="/index/work_orders">Add a Work Order</a> <a href="/index/add