limiting the drop down option of a one-to-one relationship in the admin app. is that possible ?

18 views
Skip to first unread message

Mike08

unread,
Jan 28, 2017, 9:21:34 PM1/28/17
to Django users
I currently have a model that looks like this

class modelStudent(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE,null=True, blank=True)
    school           = models.ForeignKey(modelSchool)

Now I also have a modelStudentAdmin of the above model which basically filters out the data seen by the user. The code looks like this

class modelStudentAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super(modelStudentAdmin, self).get_queryset(request)

        if request.user.is_superuser:
            return qs
        else:
            # Get the school instance
            schoolInstance = modelSchool.objects.get(user=request.user)
            qs = modelStudent.objects.filter(school=schoolInstance)
            return qs

admin.site.register(modelStudent,modelStudentAdmin)

now my question is in the admin when the user attempts to create a new modelStudent for the field "user" all available users are shown in the drop down. My question is if there is a way for me to limit/restrict that drop down to a certain value or (set that drop down value to something and then have the drop down disabled) ? I only want people creating modelStudentAdmin under their respective user field.



Melvyn Sopacua

unread,
Jan 28, 2017, 9:42:15 PM1/28/17
to django...@googlegroups.com

On Saturday 28 January 2017 18:21:34 Mike08 wrote:

 

> now my question is in the admin when the user attempts to create a new

> modelStudent for the field "user" all available users are shown in

> the drop down. My question is if there is a way for me to

> limit/restrict that drop down to a certain value or (set that drop

> down value to something and then have the drop down disabled) ? I

> only want people creating

> modelStudentAdmin under their respective user field.

 

Set a custom form, exclude the User field and in the clean method of that form, set the user to request.user.

 

--

Melvyn Sopacua

Message has been deleted

Mike08

unread,
Jan 29, 2017, 12:48:03 AM1/29/17
to Django users
Ok that makes sense.

Now I have something like this

class modelStudentAdmin(admin.ModelAdmin):
    form = modelStudentAdminForm

    #Only display students that belong to this user
    def get_queryset(self, request):
        qs = super(modelStudentAdmin, self).get_queryset(request)

        if request.user.is_superuser:
            return qs
        else:
            # Get the school instance
            schoolInstance = modelSchool.objects.get(user=request.user)
            qs = modelStudent.objects.filter(school=schoolInstance)
            return
 qs

    def get_form(self, request, obj=None, **kwargs):
        self.fields["first_name"] = "asasa"
        #modelStudentAdmin.form['first_name']="FooUSerFs"
        #modelStudentAdmin.form.cleaned_data.setdefault("first_name","dsdsds")
        #modelStudentAdmin.form.initial={"first_name":"JoeJoe"}
        #form.cleaned_data.get('player')
        return modelStudentAdmin.form



Now I am not sure how to assign values to fields in the form. Normally when assigning a value to a field in the form I would pass initial a dictionary in the constructor.
In this case it says

 'NoneType' object does not support item assignment

So my question is how can i mark a field as readonly in the form and assign an initial value ?

Melvyn Sopacua

unread,
Jan 29, 2017, 4:58:21 AM1/29/17
to django...@googlegroups.com

On Saturday 28 January 2017 21:48:03 Mike08 wrote:

> Ok that makes sense.

>

> Now I have something like this

>

> class modelStudentAdmin(admin.ModelAdmin):

> form = modelStudentAdminForm

>

> #Only display students that belong to this user

> def get_queryset(self, request):

> qs = super(modelStudentAdmin, self).get_queryset(request)

>

> if request.user.is_superuser:

> return qs

> else:

> # Get the school instance

> schoolInstance =

> modelSchool.objects.get(user=request.user) qs =

> modelStudent.objects.filter(school=schoolInstance) return qs

>

> def get_form(self, request, obj=None, **kwargs):

 

This self isn't the form. It's the model admin and should return a ModelForm. The docs I linked has a complete example you can build on.

--

Melvyn Sopacua

Reply all
Reply to author
Forward
0 new messages