开发者

Where to put generic Database Tables on Django?

I'm trying to star开发者_开发知识库t with Django.

I'm developing an App. This App is called "Directory" and will store info about websites.

I must to create a database table called "Genders", but this is a generic database table and could be used in other App.

My question... how to deal with this kind of situation in Django? And in wich model I shuld put this database table?

Best Regards,


In django, you don't (generally) create database tables. You create models, and let the django ORM create tables for you.

To prevent having two tables in your database called gender, django will prefix the model name with the name of the app. Thus, if your app was called foo, your model class was Gender, you would have foo_gender. But, you don't need to know this.

As for the specific case of Gender: I wouldn't bother storing this in the database in a special table. They don't make new genders anymore. If you need to store a field containing a gender, then just use django.db.models.CharField, with a choices=(('M',"Male"),('F',"Female")). Or if you do it lots, then create a new field.

If it is something you do need to be able to add dynamically to the database, then either abstract it out into a reusable app, or as someone else mentioned, create a sundry/project/utils app.


Django doesn't really offer you place where things that are commonly used all over the project can live. I think the most common approach to solve this is to create a 'project'-app, which holds such project-relevant things like models, templatetags or widgets for example...

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜