1.NOT NULL constraint failed;2. How do I set field defaults in admin based on the currently logged in user?

< H2 > 1.Works model has three fields: title (title), category (cagegory), author (author), where category and author are foreign keys < / H2 >

title = models.CharField (max_length=200, verbose_name=" title")
category = models.ForeignKey (Category, on_delete=models.CASCADE, blank=True, null=True, verbose_name=" Category")
author = models.ForeignKey (Author, on_delete=models.CASCADE, blank=True, null=True, verbose_name=" author")

< H2 > when you start admin to add records, just fill in the title and leave the other two items empty. Click Save and report an error, as follows: < / H2 >

IntegrityError at / admin/pattra/works/add/
NOT NULL constraint failed: pattra_works.author_id
Request Method: POST
Request URL: python.exe
Python Version: 3.6.3

< hr > < H2 > 2.provider is a foreign key of User. The default value was originally set to user [1] in the model. How to make the default value of a record dynamically associate with the user name of the current login when adding a record in admin? < / H2 >

provider = models.ForeignKey (User, on_delete=models.CASCADE, verbose_name=" uploader",

                            default=User.objects.get(id=1))

< hr >

somehow uploading pictures always indicates that the operation failed

not long after I studied django, I also looked up some content on the Internet, but I couldn"t solve it all the time. Please take the trouble to give me some advice

Mar.07,2021
Menu