简体   繁体   中英

restrict django choice field on django admin per user

I have an exam like this:

class Exam(BaseModel):
    ...
    STATE_CHOICES = (
        (PASS, PASS),
        (FAILED, FAILED),
        (GREAT, GREAT),
    state = models.CharField(max_length=15, choices=STATE_CHOICES, default=PASS)
    ...

Inside Django admin, I want the user with group X to be able to only change the state only from FAILED to PASS. and users with group Y be able to change the state from FAILED to PASS and PASS to GREAT. here is my admin.py:

@admin.register(Exam)
class ExamAdmin(NestedModelAdmin):
    list_display = ('state',)

Does anyone know a solution for it?

This might work;

class ExamForm(forms.ModelForm):
    ...
    STATE_CHOICES = (
        (PASS, PASS),
        (FAILED, FAILED),
        (GREAT, GREAT),
    state = forms.CharField(choices=STATE_CHOICES)
    class Meta:
        model = Exam
        fields = ('state',)
    ...

@admin.register(Exam)
class ExamModelAdmin(admin.ModelAdmin):
    ...
    fields = ('state',)
    list_display = ('state',)
    form = ExamForm
    ...

Sorry for giving you a bad example before, didn't have too much time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM