开发者

Django - what is considered a "Big" model?

I like to normalize things the most I can in a database. This isn't obviously the best thing for performance, but it helps with flexibility, reporting, etc. I've found it hard to justify in a recent case though which has caused me to build a model that has 25 fields in it. Is this "too big"? Or, is that quite normal. Of course the obvious answer is, "Whatever works best for you scenario.", but I intend t开发者_如何学Goo sell the software eventually, and I want whoever buys it to like the code (pay more for it).


Well, like you said: does it work in your scenario?

But to answer your question in a more appropriate way: it really depends. Since you haven't provided any information about the field you define in your model, it's hard for us to guess if it's normal.

If it really bothers you, you can try to create some extra models. For example:

class User(model):
    username = Charfield()

    # Settings for this user
    showdebug = Boolean()
    regexsearch = Boolean()
    .....
    colour = HexColor()

could become:

class UserSettings(model):
    showdebug = Boolean()
    regexsearch = Boolean()
    ......
    colour = HexColor()

class User(model):
    username = Charfield()

    settings = ForeignKey(UserSettings)


The above answer is outdated but correct :) Below is a more standard syntax for Django and here is a link to the ForeignKey docs.

To answer the question a different way, it's much more important to set accurate model parameters and to use the model as effectively as possible. Where you will start to see issues with models size is really at scale and depends on the type of DB you're using. A seasoned developer will likely write some tests to determine at what point things start breaking.

class UserSettings(models.Model):
    userBio = models.TextField(max_length=500, blank=True)

class Profile(models.Model):
    email = models.CharField(max_length=255, blank=True)
    settings = models.ForeignKey(UserSettings, on_delete=models.CASCADE)
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜