WebFaction
Community site: login faq

Hi guys,

in my admin add/change page, I want to filter the user groups that the user can see through the following:

class StudentAdmin(admin.ModelAdmin):
    inlines = [StudentProfileInline]
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.name == "groups":
            kwargs["queryset"] = Group.objects.get(name='Students')
        return super(StudentAdmin, self).formfield_for_manytomany(db_field, request, **kwargs)

    def queryset(self,request):
        return User.objects.filter(groups__name='Students')

I would get error this this part of the code kwargs["queryset"] = Group.objects.get(name='Students') complaining that 'Group object does not contain attribute all'. If I change the the filtering from 'get' to Group.objects.filter(name='Students') . No such problems.

Being curious, what is the explanation for this?

asked 11 Feb '11, 23:08

amateur
1111416
accept rate: 0%


Sounds like the result of your query returns a list of multiple objects rather than a single one. get() is used to return a single object, so when it gets more than one from the DB, trouble ensues. Usually, you don't want to use get() unless it's on a column that has a unique id (like pk), while filter() filters a list into a smaller one, even if that's a list with one object in it.

permanent link

answered 11 Feb '11, 23:22

danielquinn
1211310
accept rate: 14%

@daniel, checked my table. Theres only one record for group 'Students'. Well I guess anything goes..

(11 Feb '11, 23:27) amateur
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×909

question asked: 11 Feb '11, 23:08

question was seen: 3,741 times

last updated: 11 Feb '11, 23:27

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM