简体   繁体   English

Django OneToOne Field

[英]Django OneToOne Field

Now I'm working one url mapping. 现在我正在进行一个url映射。 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. 由于它们在url中是相同的层次结构,因此我必须创建一个类url_mapping以确保没有重复的名称。 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. 现在,当访问者使用某个网址访问我的网站时,我会匹配URL_mapping类中的网址,然后执行反向查找,查看公司,用户和商店之间的网址类型。

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? 由于User,Store和Company具有不同的视图功能,我们是否可以使用反向查找快速直接重新直接到相应的视图功能? Or should I add another field in URL_mapping saying that which url type it is? 或者我应该在URL_mapping中添加另一个字段,说明它是哪个url类型?

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. 其中url_mapping是oneToOneField。

Don't know how to quickly look up from url_mapping class now. 现在不知道如何从url_mapping类快速查找。

Thank you. 谢谢。

I understand your question as "I have a URL, and I want to go to the corresponding Store, company or the User". 我理解你的问题是“我有一个URL,我想去相应的商店,公司或用户”。

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. 显然,其中2个会给你一个错误,你不知道它映射到哪个。

Seems to me like, for what you are really looking for here, you should really use Generic Foreign Keys 在我看来,对于你在这里寻找的东西,你应该使用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. 它将具有相应的UserCompanyStore模型。

  1. I would use a SlugField in each model (Company, User, Store) as their identifier. 我会在每个模型(公司,用户,商店)中使用SlugField作为其标识符。

  2. 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. 从理论上讲,您根本不需要任何URL映射表,在处理请求的视图中,提取URL的最后一部分,这是标识公司的slug,或者是User,或Store,搜索公司,然后用户,然后存储给定slug的模型。 Stop when you find the object. 找到对象时停止。

  3. to improve speed, you can create an auxiliary model like you did and use GenericForeignKey relation as Lakshman Prasad suggested. 为了提高速度,你可以像你一样创建一个辅助模型,并使用GenericForeignKey关系,如Lakshman Prasad所建议的那样。 In this auxiliary model, again, I would use a SlugField for an identifier. 在这个辅助模型中,我再次使用SlugField作为标识符。 And if you use that, you do not need slugs in your main models. 如果您使用它,您的主模型中不需要slu。

  4. I personally think this is a bad design. 我个人认为这是一个糟糕的设计。 First, I doubt that these URLs are REST-ful. 首先,我怀疑这些URL是否完整。 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. 其次,为了实现这一点,主要模型中的模块必须在这三个模型中是唯一的,只能通过外部机制来确保,这里不能使用UNIQUE约束。 Your URL_mapping model is simply one such mechanism. 您的URL_mapping模型只是一种这样的机制。 It basically stores your slugs for the three models outside the models and, if you add the UNIQUE constraint to the SlugField in URL_mapping , makes sure the slugs are unique across your main models. 它基本上存储您的蛞蝓的车型外三款车型,如果您添加UNIQUE约束到SlugFieldURL_mapping ,确保蛞蝓是在你的主力车型唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM