limit-choices-to

Django limit_choices_to for multiple fields with “or” condition

此生再无相见时 提交于 2020-05-26 10:51:46
问题 I am trying to limit choices for a field, by checking values of the two columns, 'share_holder' and 'distributor'. If any of them is true, then I want that choice. With below version, I only got choices satisfying both conditions ('share_holder': True AND 'distributor': True). limit_choices_to={'share_holder': True, 'distributor': True} However, I need choices for ('share_holder': True OR 'distributor': True). 回答1: You can use Q objects to achieve this. from django.db.models import Q limit

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

安稳与你 提交于 2019-12-06 03:17:38
问题 I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} ) def __unicode__(self):

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

喜夏-厌秋 提交于 2019-12-04 08:53:14
I've got a couple django models that look like this: from django.contrib.sites.models import Site class Photo(models.Model): title = models.CharField(max_length=100) site = models.ForeignKey(Site) file = models.ImageField(upload_to=get_site_profile_path) def __unicode__(self): return self.title class Gallery(models.Model): name = models.CharField(max_length=40) site = models.ForeignKey(Site) photos = models.ManyToManyField(Photo, limit_choices_to = {'site':name} ) def __unicode__(self): return self.name I'm having all kinds of fun trying to get the limit_choices_to working on the Gallery 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: “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,

django: How to limit field choices in formset?

不问归期 提交于 2019-11-29 10:39:59
I'm having problems limiting the selectable choices in a formset. I have the following models: Employees, Department, Project, Projecttype, Membership, and Role. An employee can add/remove the roles that they play for a given departments project in the formset, the form should limit the selectable projects to only those belonging to the department that the employee belongs to. MODELS: class Department(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self.name class Employee(models.Model): fname = models.CharField(max_length=15) department = models.ForeignKey

django: How to limit field choices in formset?

拈花ヽ惹草 提交于 2019-11-28 03:34:21
问题 I'm having problems limiting the selectable choices in a formset. I have the following models: Employees, Department, Project, Projecttype, Membership, and Role. An employee can add/remove the roles that they play for a given departments project in the formset, the form should limit the selectable projects to only those belonging to the department that the employee belongs to. MODELS: class Department(models.Model): name = models.CharField(max_length=20) def __unicode__(self): return self