Why does adding this __init__() method break my Django model?

后端 未结 3 832
情话喂你
情话喂你 2021-01-23 07:13

This question is a follow-up to one I asked earlier here.

I have a Django model as follows:

class MyModel(models.Model):
    my_field1 = models.DateTimeF         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-23 08:16

    Using init is discouraged. According to Django docs:

    https://docs.djangoproject.com/en/1.9/ref/models/instances/#creating-objects

    from django.db import models
    
    class Book(models.Model):
        title = models.CharField(max_length=100)
    
        @classmethod
        def create(cls, title):
            book = cls(title=title)
            # do something with the book
            return book
    
     book = Book.create("Pride and Prejudice")
    

提交回复
热议问题