[英]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.