manytomanyfield

Ordered ManyToManyField that can be used in fieldsets

旧城冷巷雨未停 提交于 2019-11-28 19:23:11
问题 I've been working through an ordered ManyToManyField widget, and have the front-end aspect of it working nicely: Unfortunately, I'm having a great deal of trouble getting the backend working. The obvious way to hook up the backend is to use a through table keyed off a model with ForeignKey s to both sides of the relationship and overwrite the save method. This would work great, except that due to idiosyncrasies of the content, it is an absolute requirement that this widget be placed in a

How can I sort by the id of a ManyToManyField in Django?

折月煮酒 提交于 2019-11-28 03:17:06
问题 I've got a ManyToManyField in a user object and it's used to map the users that user is following. I'm trying to show a subset list of who they have most recently followed. Is there a trick in .order_by() that will allow me to sort by the id of the ManyToManyField? The data is there, right? # (people the user is following) following = models.ManyToManyField(User, related_name="following", blank=True) theuser.following.filter(user__is_active=True).order_by("user__id") That will give me a list

How to make recursive ManyToManyField relationships that have extra fields symmetrical in Django?

旧巷老猫 提交于 2019-11-28 01:27:56
问题 class Food_Tag(models.Model): name = models.CharField(max_length=200) related_tags = models.ManyToManyField('self', blank=True, symmetrical=False, through='Tag_Relation') def __unicode__(self): return self.name class Tag_Relation(models.Model): source = models.ForeignKey(Food_Tag, related_name='source_set') target = models.ForeignKey(Food_Tag, related_name='target_set') is_a = models.BooleanField(default=False); # True if source is a target has_a = models.BooleanField(default=False); # True

Django Admin ManyToManyField

只愿长相守 提交于 2019-11-27 13:08:33
问题 I've made a model (models.py): class opetest(models.Model): name = models.CharField(max_length=200) author = models.ForeignKey(User, related_name='author') description = models.TextField(u'Test description', help_text = u'Some words about quiz') pub_date = models.DateTimeField('date published', blank=False) vacancies = models.ManyToManyField(Vacancy, blank=True) students = models.ManyToManyField(User, blank=True, related_name='opetests') #This field I want to edit on "User change page"

Django: ManyToMany filter matching on ALL items in a list

放肆的年华 提交于 2019-11-27 09:03:19
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 books with just 2 authors. candidate_books = Book.objects.annotate(c=Count('authors')).filter(c=len(target