开发者

django not taking in consideration model fields declared in __init__

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜