Saturday, January 28, 2017

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

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.



--
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/efb6b1cb-ca74-4a6e-bcaa-2174e3a8d717%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment