Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I give the same name for different Models in different apps?

Tags:

python

django

Can I give the same name for different Models in different apps? and What conflicts can happen?

After I have a try.. I got this Error:

Error: One or more models did not validate:
playlist.playlist: Accessor for field 'user' clashes with related field 'User.playlist_set'. Add a related_name argument to the definition for 'user'.
audio_playlist.playlist: Accessor for field 'user' clashes with related field 'User.playlist_set'. Add a related_name argument to the definition for 'user'.
like image 424
Pol Avatar asked Oct 22 '10 16:10

Pol


People also ask

Should Django app name be plural or singular?

Correct Model Naming It is generally recommended to use singular nouns for model naming, for example: User, Post, Article. That is, the last component of the name should be a noun, e.g.: Some New Shiny Item.

What should be an app Django?

A Django app is a small library representing a discrete part of a larger project. For example, our blog web application might have an app for posts , one for static pages like an About page called pages , and another app called payments to charge logged-in subscribers.

What is Django app and project?

An app in Django is a sub-module of a project, and it is used to implement some functionality. Now, you can refer to an app as a standalone python module that is used to provide some functionality to your project. We can create multiple apps within a single Django project.


2 Answers

Of course you can do that. There won't be any conflicts because tables are stored internally as appname_modelname; let's say you have a model named Post in an app named blog and a model named Post in an app named messages. Tables will be stored as blog_post and messages_post. Also, the python classes are named project.blog.models.Post and project.messages.models.Post, so no conflict here either.

EDIT: Also, to be able to import them both in one file, use something like this:

import blog.models.Post as BlogPost
import messages.models.Post as MessagesPost

(or any names that make sense)

like image 117
Gabi Purcaru Avatar answered Oct 05 '22 11:10

Gabi Purcaru


It's true that at the database level this will cause no issues.

You WILL run into problems with your URL routes and internal view names, though!

Assume you have two apps--"blog" and "comments" and each has a model named Message, and each Message model has a corresponding ModelViewSet (BlogMessageViewSet and CommentMessageViewSet, respectively)

The Django router will automatically name your view names message-list, message-detail, etc... with no regard for which app that model is in. So you will have collisions after you load these into your router since you have two models named 'Message'. For example, if in urls.py my router looked like this:

router = routers.DefaultRouter()
router.register(r'blog/message', BlogMessageViewSet)
router.register(r'comment/message', CommentMessageViewSet)

Then internally both the routes for /blog/message and /comment/message will point to /comment/message since that was registered last with the router and internally the view names will be message-list, message-detail, etc...

You can resolve this issue by giving a different base name to one of your views though like this:

router = routers.DefaultRouter()
router.register(r'blog/message', BlogMessageViewSet, view_name='blogmessage')
router.register(r'comment/message', CommentMessageViewSet)

Though this will require you to explicit define the url as a HyperlinkedIdentityField on the serializer for BlogMessageViewSet if you are using a HyperlinkedModelSerializer and to make all references to this view in urlresolvers.reverse() or other url-based tools use the view name 'blockmessage.'

like image 25
user1847 Avatar answered Oct 05 '22 11:10

user1847