Saturday, January 28, 2017

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

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 ?

  

On Saturday, January 28, 2017 at 6:21:34 PM UTC-8, Mike08 wrote:
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/8dd86eb8-7670-4058-bc30-abdf6c81e97b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment