Tuesday, January 31, 2017

Re: Limiting choices in a lookup by exclusion

django doesn't have an ne operator in the orm. You want to use a negated Q object for your limit_choices_to. try:

from django.db.models import Q

phone = models.ManyToManyField(Phone, limit_choices_to = ~Q(type_id = 'C'))

On Wednesday, February 1, 2017 at 6:17:52 AM UTC+2, Gordon Burgess wrote:

I have this code, and with Django 1.10 it works as expected:

class Location(models.Model):
    name = models.CharField(max_length = 32, unique = True)
    street = models.CharField(max_length = 32)
    detail = models.CharField(max_length = 32, blank = True, null = True)
    city = models.CharField(max_length = 32)
    state = USStateField()
    zip = USZipCodeField()
    phone = models.ManyToManyField(Phone, limit_choices_to = {'type_id':'H'})

but what I'd like to do is restrict the choices of phone numbers for Locations to those that aren't 'C' (cell phones) - I've found some hints, but they're all for older versions of Django - it seems like:

    phone = models.ManyToManyField(Phone, limit_choices_to = {'type_id__ne':'C'})

ought to work - but this provokes a TypeError:

Related Field got invalid lookup: ne

Thanks!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/37b94253-fd0e-479f-8c0d-ffe262b1dcd4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment