简体   繁体   English

如何将两个不同模型的uuid和id放在同一个网址中?

[英]How to put two different model's uuid and id in a same url?

I have two different model linked by a ForeignKey one contains a certain uuid and the other has a certain id . 我有两个通过ForeignKey链接的不同模型,一个包含一个特定的uuid ,另一个包含一个特定的id I'd like to be able to put these in the same URL. 我希望能够将它们放在相同的URL中。

Here's the models.py : 这是models.py

class Creation(models.Model):
    ...
    uuid = models.UUIDField(default=uuid.uuid4, unique=True)

class User(models.Model):
    ...
    creation = models.ForeignKey(Creation, null=True)

Here's what the URL pattern should look like : URL模式如下所示:

url(r'^edit/(?P<id>\d+)/(?P<uuid>[^/]+)/$', views.edit, name='edit'),

Views.py Views.py

def edit(request, id, uuid):
    user_uuid = User.objects.filter(id=id)
    user = get_object_or_404(User, id=id, uuid=user_uuid.creation.uuid)

As you can see the view function doesn't make any sense since I don't see how what I'm trying to do should work but the User should be the id in the URL and the Creation should be the uuid since each user can have many Creation s. 如您所见,视图函数没有任何意义,因为我看不到我要做什么,但是User应该是URL中的id,而Creation应该是uuid,因为每个用户都可以有很多Creation

How can I achieve this? 我该如何实现?

Your question is quite hard to understand, but I think what you want is: 您的问题很难理解,但我您想要的是:

user = User.objects.get(id=id)
creation = user.creation_set.filter(uuid=uuid)

Note of course though that since uuid is unique, there's no need to use the User ID in the query at all; 当然,请注意,由于uuid是唯一的,因此根本不需要在查询中使用用户ID。 you could drop it from the URL altogether, use just the uuid to get the Creation, then get the user via creation.user . 您可以将其完全从URL中删除,仅使用uuid来获取Creation,然后通过creation.user获取用户。

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

相关问题 如何在两个单独的数据框中保持相同的 uuid? - How to keep same uuid in two separate dataframes? 如何在同一模型中使用两个不同的模型序列化器? - How Can I Use Two Different Model Serializers With the Same Model? 如何在 Django 中正确使用 UUID id 作为 url 参数? - How do I properly use a UUID id as a url parameter in Django? 两个不同的深度学习框架如何使用同一模型? - how can two different deep learning frameworks use the same model? 来自两个不同模型的同一模型中的两个外键 - Two foreign key in the same model from two different model 使用相同的模型创建两个不同的表 - Create two different tables using the same model 使用相同的模型更新两个不同类中的tableView - Update tableView in two different classes with same model 使用 Keras 训练具有两个不同输出的相同 model - Training the same model with two different outputs with Keras 具有相同 URL 的两个不同 XML 命名空间 - Two different XML namespaces with the same URL 如何删除(并放入新的df)在第一列中具有相同值但在第三列中具有相同值的所有记录? - How to remove (and put to new df) all the records that have the same value in two first columns but different in third one?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM