开发者

django not taking in consideration model fields declared in __init__

开发者 https://www.devze.com 2023-01-15 00:29 出处:网络
When using Model class like this: class MyModel(models.Model): def __init__(self, *args, **kwargs): 开发者_Python百科self.myfield = models.Field()

When using Model class like this:

class MyModel(models.Model):
    def __init__(self, *args, **kwargs):
        开发者_Python百科self.myfield = models.Field()
        super(MyModel, self).__init__(*args, **kwargs)

It doesn't take into consideration myfield(in the admin form, when saving the object... )

But if i declare like that:

class MyModel(models.Model):
    myfield = models.Field()

It works just fine.

Why?

Edit

I think i have a good reason: I have an abstract class UploadItem that defines a field called file like this: self.file = models.FileField(upload_to=upload_to) As you can see, in each child class, i have to call parent init method with appropriate upload_to variable(say 'videos' for Video model). So i cannot do it the normal way.


Because the Django ORM code does some serious meta-magic during class definition (just browse the django/db code to see how magic). You are doing an end-run around that magic by creating fields on the fly in the __init__() function.

Is there a really good reason for not creating the class in the normal way? If not, then do it the normal way. If you do have a good reason then get ready to get into the really deep end of the pool -- both of Python and Django.


Setting a dynamic path for the upload_to attribute is absolutely not a good reason for wanting to muck around with model field declaration.

This is something that Django handles already - if you set upload_to to a callable, you can return the correct value dependent on the model instance. See the documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消