Django OneToOne Field
Now I'm working one url mapping. Let's say I have three classes, company, user, and store, and my goal is that their urls will be in the same hierarchy. Since they are the same hierarchy in urls, I have to create a class url_mapping to ensure there is no duplicate name. Let's me give a more concrete problem I have.
Class Company(models.Model):
company_name = models.CharField(max_length=30)
url_mapping = models.OneToOneField(URL_mapping)
Class User开发者_运维问答(models.Model):
user_name = models.CharField(max_length=30)
url_mapping = models.OneToOneField(URL_mapping)
Class store(models.Model):
store_name = models.CharField(max_length=30)
url_mapping = models.OneToOneField(URL_mapping)
Class URL_mapping(models.Model):
url = models.CharField(max_length=30)
Now, when a visitor access to my site with certain url, I'll match the url in URL_mapping class, and then do the reverse lookup and see which type of url between company, user, and store it is.
Since User, Store, and Company have different view function, is it possible that we can quickly re-directly to the corresponding view function quickly using reverse lookup? Or should I add another field in URL_mapping saying that which url type it is?
The example is
http://www.example.com/levis -> will handle by brand_views
http://www.example.com/david -> will handle by user_views
http://www.example.com/mancy -> will handle by store_views
In database, we will have
url_mapping
id:1, name:levis
id:2, name:david
id:3, name:mancy
user
id:1, name:david, url_mapping:2
brand
id:1, name:levis, url_mapping:1
store
id:1, name: mancy, url_mapping:3
Where url_mapping is oneToOneField.
Don't know how to quickly look up from url_mapping class now.
Thank you.
I understand your question as "I have a URL, and I want to go to the corresponding Store, company or the User".
You can do that using
URL_mapping.objects.get(url).user
URL_mapping.objects.get(url).store
URL_mapping.objects.get(url).company
Clearly 2 of these will give you an error and you wouldn't know which it maps to.
Seems to me like, for what you are really looking for here, you should really use Generic Foreign Keys
So, then you will be able to do:
URL_mapping.objects.get(url)
which will have the corresponding User
, Company
or the Store
model.
I would use a
SlugField
in each model (Company, User, Store) as their identifier.theoretically, you do not need any URL mapping tables at all, in the view that handles the requests, extract the last part of the url, which is a slug identifying a Company, or a User, or a Store, and search Company, then User, and then Store models for the given slug. Stop when you find the object.
to improve speed, you can create an auxiliary model like you did and use
GenericForeignKey
relation as Lakshman Prasad suggested. In this auxiliary model, again, I would use aSlugField
for an identifier. And if you use that, you do not need slugs in your main models.I personally think this is a bad design. First, I doubt that these URLs are REST-ful. Second, for this to work, the slugs in your main models have to be unique across these three models, which can be ensured by only an external mechanism, you cannot use a
UNIQUE
constraint here. YourURL_mapping
model is simply one such mechanism. It basically stores your slugs for the three models outside the models and, if you add theUNIQUE
constraint to theSlugField
inURL_mapping
, makes sure the slugs are unique across your main models.
精彩评论