Setting default value for Foreign Key attribute

后端 未结 8 805
后悔当初
后悔当初 2020-12-08 01:32

What is the best way to set a default value for a foreign key field in a model? Suppose I have two models, Student and Exam with student having

8条回答
  •  天涯浪人
    2020-12-08 02:12

    I'm looking for the solution in Django Admin, then I found this:

    class YourAdmin(admin.ModelAdmin)
    
        def get_changeform_initial_data(self, request):
            return {'owner': request.user}
    

    this also allows me to use the current user.

    see django docs

提交回复
热议问题