manytomanyfield

How to write migration to change primary key of model with ManyToManyField

断了今生、忘了曾经 提交于 2019-12-01 19:36:04
问题 I have a UserProfile model that refers to my User model with a OneToOneField . I also use the post_save signal to auto create the UserProfile when the user is created. This works great apart from when creating a user through the admin (where I use an inline) when I get an error about duplicate profile. This answer recommends setting the primary key to be the OneToOneField referring to user. So before: class UserProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL) # ...

Django: “limit_choices_to” doesn't work on ManyToManyField

拈花ヽ惹草 提交于 2019-11-30 21:10:52
I am running Django 1.1 and cannot get the "limit_choices_to" option for my ManytoManyField to work. I have two models: class MemberPhoto(ImageModel): title = models.CharField(_('title'), max_length=255, blank=True, null=True) caption = models.CharField(_('caption'), max_length=255, blank=True, null=True) date_added = models.DateTimeField(_('date added'), default=datetime.now, editable=False) member = models.ForeignKey(User) def __unicode__(self): return u'%s (%s)' % (self.member.username, self.id) and class lock(models.Model): user = models.ForeignKey(User, related_name="owner") to_user =

django manytomanyfield .add() method

对着背影说爱祢 提交于 2019-11-30 20:34:32
Suppose i have : class Album(models.Model): photos = models.ManyToManyField('myapp.Photo') photo_count = models.IntegerField(default = 0) class Photo(models.Model): photo = models.ImageField(upload_to = 'blahblah') What is want is, everytime i call .add() method, increment the photo_count on Album class, so i'm thinking to override .add() method. The problem is, i can't import the .add() 's class since it is inside a method, so it is like def->class->def . So is there anyway to override .add() ? Or there is a better way to do this? You can use django's signals and write a signal handler that

Django: “limit_choices_to” doesn't work on ManyToManyField

可紊 提交于 2019-11-30 17:11:23
问题 I am running Django 1.1 and cannot get the "limit_choices_to" option for my ManytoManyField to work. I have two models: class MemberPhoto(ImageModel): title = models.CharField(_('title'), max_length=255, blank=True, null=True) caption = models.CharField(_('caption'), max_length=255, blank=True, null=True) date_added = models.DateTimeField(_('date added'), default=datetime.now, editable=False) member = models.ForeignKey(User) def __unicode__(self): return u'%s (%s)' % (self.member.username,

How do I remove multiple objects in a ManyToMany relationship based on a filter?

女生的网名这么多〃 提交于 2019-11-30 03:28:41
Given these two Models: class Item(models.Model): timestamp = models.DateTimeField() class Source(models.Model): items = models.ManyToManyField(Item, related_name="sources") I can find all of a Source's Items before a given time using this: source.items.filter(timestamp__lte=some_datetime) How do I efficiently remove all of the items that match that query? I suppose I could try something like this: items_to_remove = list(source.items.filter(timestamp__lte=some_datetime)) source.items.remove(*items_to_remove) but that seems bad. Note that I do not want to delete these items, since they may also

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

你离开我真会死。 提交于 2019-11-29 15:20:51
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 the most part, creating a Post object and the two Icon objects. However, if the icon_id is, say, 1 in

django display content of a manytomanyfield

☆樱花仙子☆ 提交于 2019-11-29 14:13:54
问题 I started using django framework just a few days ago and i desperately need some help with my application. It consists of client,project,admin,and admin payment classes where the admin_payment holds the ids of the admins and projects among other stuff. My question is how i can display the "administrator's name" of each "project" in my admin listing of projects? the project class itself does not hold the administrator ids (the Admin_Payment does) Currently i have the following structure:

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

早过忘川 提交于 2019-11-29 09:54:15
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 of the users the user is following but ordered by when they joined. I want the order of the following

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

一笑奈何 提交于 2019-11-29 07:55:49
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 if source has a target I want to be able to get the relations between Food_Tags like: >>> steak = Food

Django Admin ManyToManyField

给你一囗甜甜゛ 提交于 2019-11-28 20:30:17
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" estimate = models.IntegerField(default = 0, help_text = u'Estimate time in hours. \'0\' - unlimited') then I