获取Django对象的唯一引用ID

时间:2016-03-07 16:41:09

标签: python django

我想在我的Django项目中创建一个函数,以确保加入我网站的每个用户的参考ID的唯一性,但是当点击join时,我收到此错误:

Join matching query does not exist.

这是我的功能:

def get_ref_id():
    ref_id = str(uuid.uuid4())[:10].replace('-','').lower()
    id_exists = Join.objects.get(ref_id=ref_id)
    if id_exists:
        get_ref_id()
    return ref_id

1 个答案:

答案 0 :(得分:2)

如果查询不存在,django模型上的- (void) application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification { CustomNotificationManager *notificationManager = [[CustomNotificationManager alloc] init]; [notificationManager handleLocalNotification:notification]; } 将引发异常。所以这条线没有意义:

.get

可能你想要

id_exicts = Join.objects.get(ref_id=ref_id)

或者,您可以从id_exicts = Join.objects.filter(ref_id=ref_id).exists() 捕获Join.DoesNotExist异常并以此方式构建逻辑。