0

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?

3
  • 5 fields? not 4? Commented Nov 21, 2018 at 9:54
  • The best way is to add deadline and lecture in the views.py. In your POST method you can add them. If you are having a problem in adding in POST function, you can add your views.py to understand Commented Nov 21, 2018 at 10:47
  • @BidhanMajhi, are you sure? We are talking about admin interface not a custom view. Commented Nov 21, 2018 at 10:48

1 Answer 1

1

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"]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.