Django

Code

Ticket #1891 (reopened)

Opened 3 years ago

Last modified 4 months ago

'distinct':True no longer usable in limit_choices_to

Reported by: mattimustang@gmail.com Assigned to: nobody
Milestone: Component: Core framework
Version: Keywords:
Cc: mattimustang@gmail.com, gary.wilson@gmail.com Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 0 Patch needs improvement: 1

Description

My app has the following models similar to:

PROXY_GROUPS = ('Terminal Server', 'Socks Proxy', 'Web Proxy')

class Group(models.Model):
    name = models.CharField(maxlength=32)

class Asset(models.Model):
    name = models.CharField(maxlength=32)
    groups = models.ManyToManyField(Group)

class Proxy(models.Model):
    asset = models.ForeignKey(Asset, limit_choices_to={'groups__name__in': PROXY_GROUPS, 'distinct':True})
    port = models.PositiveIntegerField()

Which spews an error when I try to add a Proxy object in the admin. The error is something to do with 'distinct' not being a valid field (Sorry i can post the exact error later).

How can I achieve the equivalent to this in the current post-mr trunk?

From #django earlier today:

malcolmt	mattimustang_: I think you've found a bug. You're right, distinct has become a method on QuerySets, not a filter parameter now. So you may be temporarily stuck. Can you file a ticket so we don't forget to fix this somehow, please?

Attachments

limit_choice_to.diff (5.8 kB) - added by Alex on 03/11/08 12:44:14.
Patch against qs-rf

Change History

05/16/06 07:56:05 changed by anonymous

Sorry, but sqlite does not enforce foreign keys at all, see http://www.sqlite.org/omitted.html

quoting:

SQL Features That SQLite Does Not Implement

Rather than try to list all the features of SQL92 that SQLite does support, it is much easier to list those that it does not. Unsupported features of SQL92 are shown below. The order of this list gives some hint as to when a feature might be added to SQLite. Those features near the top of the list are likely to be added in the near future. There are no immediate plans to add features near the bottom of the list.

FOREIGN KEY constraints FOREIGN KEY constraints are parsed but are not enforced.

05/16/06 07:58:34 changed by anonymous

Sorry, the previous text was meant for a different bug. Please ignore it ...

05/16/06 09:50:09 changed by jkocherhans

  • status changed from new to closed.
  • resolution set to invalid.

I don't see how distinct=True makes any sense in the context of limit_choices_to. The result of limit_choices_to will have to include the primary key (that's the whole point of ForeignKey) so a SELECT DISTINCT will never return fewer rows than a plain old SELECT. I'm closing this for now. Please re-open it if you think I'm missing something.

05/17/06 01:41:25 changed by mattimustang@gmail.com

  • status changed from closed to reopened.
  • resolution deleted.

Try this:

Create an Asset in admin (in the example models above) called proxy1.

Add both Socks Proxy and Web Proxy groups to it.

Then without distinct=True, when you go to create a Proxy object you see the proxy1 Asset twice in the dropdown in the admin. Using distinct=True in Pre-MR django reduced this to 1 proxy1 item in the dropdown.

(follow-up: ↓ 9 ) 05/17/06 10:51:20 changed by jkocherhans

Ahhh I see now. groups__name__in is causing a join between the Proxy table and the Asset <-> Group many to many table. The extra Group objects are making extra distinct rows.

Trying to shoehorn all the new manager methods into kwargs is not going to be pretty. What if limit_choices_to was deprecated in favor of passing a QuerySet as the choices argument?

For example:

PROXY_GROUPS = ('Terminal Server', 'Socks Proxy', 'Web Proxy')

class Group(models.Model):
    name = models.CharField(maxlength=32)

class Asset(models.Model):
    name = models.CharField(maxlength=32)
    groups = models.ManyToManyField(Group)

class Proxy(models.Model):
    asset = models.ForeignKey(Asset, choices=Asset.objects.filter('groups__name__in': PROXY_GROUPS).distinct())
    port = models.PositiveIntegerField()

05/17/06 18:11:14 changed by mattimustang@gmail.com

Using a a QuerySet? is what I discussed with malcomt on #django but we didn't get too far with it. As long as it could handle the choices changing that would be great, ie. adding/removing assets from the PROXY_GROUPS. Would it only take a QuerySet? or any callable?

05/17/06 20:04:57 changed by Malcolm Tredinnick <malcolm@pointy-stick.com>

I had a "doh!" moment about this bug in the shower this morning: we don't need to make "distinct" something that is specified by the user. We can work out precisely when it is required and just Do The Right Thing(tm).

If the limit_choices_to attribute adds a ForeignKey? field to the query that does not have unique=True, then we should be using "select distinct". Otherwise, a simple "select" does the trick. There are some subtleties such as if we include a combination of fields that together comprise a "unique_together" block, then we don't need "distinct" any longer (because it is back to being unique by default).

Although the explanation looks complex, the code is probably not going to be too bad (although adding anything to django/db/models/query.py makes my head hurt). I'll look at writing a patch for this when I get some time (probably on the weekend, since it's queued up behind some other Django work).

05/22/06 08:34:43 changed by mattimustang@gmail.com

  • cc set to mattimustang@gmail.com.

Would it make sense to combine 'choices' and 'limit_choices_to' into one field option?

(in reply to: ↑ 5 ) 01/24/07 21:12:10 changed by Gary Wilson <gary.wilson@gmail.com>

  • cc changed from mattimustang@gmail.com to mattimustang@gmail.com, gary.wilson@gmail.com.
  • stage changed from Unreviewed to Design decision needed.

Replying to jkocherhans:

What if limit_choices_to was deprecated in favor of passing a QuerySet as the choices argument?

This would be cool.

10/26/07 17:05:18 changed by SmileyChris

  • keywords set to qs-rf.

Tagging for queryset refactor - maybe something that Malcolm wants to look at. I also like the idea of deprecating limit_choices_to and adding the option of passing a QuerySet? to choices

03/11/08 12:44:14 changed by Alex

  • attachment limit_choice_to.diff added.

Patch against qs-rf

03/11/08 12:46:44 changed by Alex

I have attached a patch which should allow you to pass either a callable or a QuerySet? to the choices parameter of a relational field, my one concern however is that it is not possible to use the limit/offset syntax with this because the QuerySet? is evaluated and that would cause the queryset to be evauated at load time, rather than each time the choices need to be generated.

03/11/08 17:52:08 changed by Alex

  • needs_better_patch set to 1.
  • has_patch set to 1.
  • needs_docs set to 1.

03/17/08 22:06:56 changed by gwilson

  • keywords changed from qs-rf to nfa-fixed.
  • stage changed from Design decision needed to Fixed on a branch.

On newforms-admin, you can use ModelAdmin.get_form to return a custom from with a custom model choice field that makes use of the queryset argument.

03/17/08 22:39:51 changed by mtredinnick

  • stage changed from Fixed on a branch to Accepted.

That solution only applied to admin. limit-choices-to is used for more than just the admin interface... it's useful wherever you extract the list of options for a ForeignKey.

08/01/08 10:56:57 changed by brosner

  • keywords deleted.

Add/Change #1891 ('distinct':True no longer usable in limit_choices_to)




Change Properties
Action