Halil Yıldırım
Halil Yıldırım

Reputation: 45

Using database for forms django

I have an area where teachers can put homeworks for students. Students can see these homeworks and when they click "send homework" button. they can fill a form so Teachers can see the homeworks from admin panel. I have class to post homework. I have 4 fields in class to post homework.

Fields = ["student number","Deadline","Lecture","Files"]

when students posting their homeworks, I want only use 2 ["student number","files"] fields for the form and I want other fields ["deadline","Lecture"] to be filled automatically from database.

What is the best way do it?

Upvotes: 0

Views: 37

Answers (1)

dani herrera
dani herrera

Reputation: 51665

Quoting django model clean method docs:

Model.clean() This method should be used to provide custom model validation, and to modify attributes on your model if desired. For instance, you could use it to automatically provide a value for a field, or to do validation that requires access to more than a single field.

For your scenario:

import datetime
from django.db import models

class Homeworks(models.Model):
    ...
    def clean(self):
        self.Deadline = datetime.date.today() + some delta time
        self.Lecture = ...

Also, remove this 'self-filled' fields from admin form interface:

class HomeworksAdmin(admin.ModelAdmin):
    fields = ["student number","Files"]

Upvotes: 1

Related Questions