Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Update only specific fields in a models.Model Django

I have an APP to rate supervisors to personnel in Django.

I have model in models.py in my app Django :

class SuperviserModel(models.Model):
    name = models.TextField(db_collation='C')  
    family = models.TextField(db_collation='C')  
    score = models.FloatField(db_column='score ', blank=True, null=True)  # 
    def __str__(self)-> str:
        return f' -  {self.family}  - {self.name}'
    class Meta:
        managed = False
        db_table = 'personnel'

And in admin.py:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@admin.register(SuperviserModel)
class MySuperviserModelAdmin(admin.ModelAdmin):
    list_display = ['name','family','score']
    sortable_by = ['name','family']
    search_fields=['name', 'family']

I want when superviser user edit personnel just update score field, And cant update name and family field.

How can I do?

>Solution :

Specify score as only .fields [Django-doc]:

@admin.register(SuperviserModel)
class MySuperviserModelAdmin(admin.ModelAdmin):
    list_display = ['name', 'family', 'score']
    sortable_by = ['name', 'family']
    search_fields = ['name', 'family']
    fields = ['score']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading