Django:从模型字段数据中获取模型字段数据

时间:2016-04-19 10:40:49

标签: django django-models django-database django-datatable

我在views.py中有这样的东西。

self.imageView.center = CGPointMake(MAX(0.0, MIN(location.x - previousLocation.x, self.sub_View.bounds.size.width)), MAX(0.0, MIN(location.y - previousLocation.y, self.sub_View.bounds.size.height)));
  

如何进行查询或获取新创建的引荐来源   引荐?

要进一步了解我的意思,请查看下面的图片 enter image description here

我希望你理解我的问题。

1 个答案:

答案 0 :(得分:1)

我猜代码中有一些错误。

如果form.is_valid():

    ref_user = User.objects.get(
    username=form.cleaned_data['referrer'])

    if User.objects.filter(username=ref_user).exists():

        user = User.objects.create_user(
        username=form.cleaned_data['username']
        )   

        direct = DirectReferral.objects.create(
            name = user,
            referrer = ref_user # New created referrer
        )

        if DirectReferral.objects.filter(referrer=ref_user).exists():

            # Here you are checking for something you just created. 
            # I guess this should be 
            # DirectReferral.objects.filter(name=ref_user).exists()

            # Get the referrer of the newly created referrer in DirectReferral database

            indirect = IndirectReferral.objects.create(
                name = user,
                referrer = # This should be the referrer of the newly created referrer
            )

如果您打算找到当前创建的用户的推荐用户的推荐人,您可以这样做。

如果form.is_valid():

    ref_user = User.objects.get(
    username=form.cleaned_data['referrer'])

    if User.objects.filter(username=ref_user).exists():

        user = User.objects.create_user(
        username=form.cleaned_data['username']
        )   

        direct = DirectReferral.objects.create(
            name = user,
            referrer = ref_user # New created referrer
        )

        try:
            referral_of_ref_user = DirectReferral.objects.get(name=ref_user).referrer
        except DirectReferral.DoesNotExist:
            # Do what needs to be done if direct referral not exist.
            print "No referrer"
            # Get the referrer of the newly created referrer in DirectReferral database
        else:
            indirect = IndirectReferral.objects.create(
                name = user,
                referrer = referral_of_ref_user
            )

修改

通过对代码的最小编辑:

如果form.is_valid():

    ref_user = User.objects.get(
    username=form.cleaned_data['referrer'])

    if User.objects.filter(username=ref_user).exists():

        user = User.objects.create_user(
        username=form.cleaned_data['username']
        )   

        direct = DirectReferral.objects.create(
            name = user,
            referrer = ref_user # New created referrer
        )

        if DirectReferral.objects.filter(name=ref_user).exists():

            # Get the referrer of the newly created referrer in DirectReferral database

            indirect = IndirectReferral.objects.create(
                name = user,
                referrer = DirectReferral.objects.filter(name=ref_user)[0].referrer
            )