Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django view, models, form, app naming

I have a question: "How should I name apps, views, models, forms, fields, etc?"

For example I have a browser game that has a mailbox implemented.

App cannot be named mailbox so I name it pm_box (is that good?)

Then I have to create model where all the messages are held. What model name should be? Message or Messages? It should have a boolean field which is True if message has already been read. Should that field be called read or is_read?

Then I have a view that lists the messages. Should I name it list_messages, message_list, message_list_view or list_messages_view (if I do not specify inbox/outbox)?

The the form for new message input data validation. Should the form be named NewMessageForm, MessageWritingForm..?

After that I want to keep track of timestamps for every player. For this purpose I have Player model (or should it be Players)? With OneToOne field to the user and OneToOne field to Timestamp model (or timestamps) that has fields: online, last_pm_sent, some_action.

Thanks for all your answers. I have already read django styling documentation and pep, however, nowhere these things are specified.

EDIT: The project name (in pyCharm) is my game name. How should I name the first app and where should I hold the Player(s) model (in which app) which is UserProfile as well.

like image 945
aemdy Avatar asked Nov 15 '11 13:11

aemdy


People also ask

How to create a form from a model in Django?

For this reason, Django provides a helper class that lets you create a Form class from a Django model. The generated Form class will have a form field for every model field specified, in the order specified in the fields attribute. Each model field has a corresponding default form field.

Why can’t I create another model field called author in Django?

In Django, this isn’t usually permitted for model fields. If a non-abstract model base class has a field called author, you can’t create another model field or define an attribute called author in any class that inherits from that base class. This restriction doesn’t apply to model fields inherited from an abstract model.

How do I build my own application in Django?

In practice, however, Django applications are made up of many different files. When building your own applications, follow common file naming conventions. Start with the framework Django provides via manage.py startapp <foo> and build out from there as needed.

What is MVC in Django?

In this entry, we are about to learn very important things about Django App. If you don’t understand how URLs, VIEWS, MODELS & TEMPLATES work and relate then I’ll assure you you will have dead-end soon so it is important you understand this flow. Now Django uses the Model View Control (MVC) pattern.


1 Answers

Alright starting with what you've named, it must not be pm_box, try naming discretely, if something is keyword try finding an appropriate synonym, here PersonalMessage and your view folder will be personal_messages while file will be usually good like single word, eg. enlist.html, display.html, etc. will be good, we mostly follow this structure in django:

Model Class Name

Singular, If mutliple used upper letter camel casing -> Example: Person, User, Subject, StudentSubject, StudentGuardian etc.

Form Class Name:

Relevant Model Class Followed by Form -> Example: PersonForm, UserForm, SubjectForm etc.

Boolean Variables:

Proper with is_ or has_ prefix -> Example: is_present, is_available, is_online, has_parent, etc.

Views:

Put in folder plural of model, but instead of upper letter use underscore(_) after every word (in case of two letters) file name will be like function name -> Example: people/index.html, people/detail.html, student_guardians/display_fee.html etc.

Though you may not entirely need a new model to keep 1-to-1 relationship, a better approach would be to add more fields to current table. Timestamp conventions could be like, last_visited_at, last_played_at, etc.

You could give app a name by defining in model as:

class UserProfile(models.Model):
    pass
    class Meta:
        app_label = 'Your Application Name'
like image 94
Usman Chaudhry Avatar answered Sep 17 '22 23:09

Usman Chaudhry