如何从django中的多个模型中选择列?

时间:2016-03-09 10:53:21

标签: python django

models.py

class Custom_user_model(User):

    daily_target = models.IntegerField()
    monthly_target = models.IntegerField()
    yearly_target = models.IntegerField()
    weekly_target = models.IntegerField()
    call_target = models.IntegerField()
    email_target = models.IntegerField()
    meeting_target = models.IntegerField()
    added_under = models.IntegerField()
    profile_pic = models.TextField()
    doj = models.DateTimeField(default='')
    location_id = models.IntegerField()
    locked = models.BooleanField()
    default_currency = models.IntegerField()
    date_change_permission = models.BooleanField()
    deal_back_log = models.BooleanField()
    created_date=models.DateTimeField(auto_now_add=True)
    role_id=models.ForeignKey('user_Roles')
    profile_pic = models.FileField(upload_to='.')
    objects = UserManager()

class Deal(models.Model):

    a_choices = ((0,'yes'),(1,'no'))
    approved = models.IntegerField(choices=a_choices,default=1)
    user_id = models.ForeignKey('Custom_user_model')
    company_id = models.IntegerField()
    contact_id = models.IntegerField()
    deal_title=models.CharField(max_length=200)
    deal_value = models.CharField(max_length=20)
    currency_id = models.IntegerField()
    process_id = models.IntegerField()
    expected_close_date = models.DateField(default='')
    closed_date = models.DateField()
    deal_milestone=models.IntegerField()
    created=models.DateTimeField(auto_now_add=True)
    last_modified=models.DateTimeField(auto_now_add=True)
    s_choices = ((0,'active'),(1,'won'),(2,'junk'),(3,'lost'))
    status = models.IntegerField(choices=a_choices,default=0)
    type = models.CharField(max_length=50, default='deal')



class user_Roles(models.Model):
    code = models.CharField(max_length=20)
    description = models.CharField(max_length=30)
    permitted_menus = models.CharField(max_length=200)
    created = models.DateTimeField(auto_now_add=True)

views.py

在这里,我编写了代码来获取三个模型的列。但是

Deal.objects.filter(user_id__role_id_id=1).select_related()一无所返,Deal.objects.filter(user_id__role_id_id=1).select_related().values()仅返回交易模型中的字段。当将关系指定为值时,它会显示“没有字段错误”(' Custom_user_model__doj')。如何从多个模型中选择字段?

def get_all_normal_users(request,start_date=None,end_date=None):

    query = Deal.objects.filter(user_id__role_id_id=1).select_related().values()
    start_date_range = (
            # The start_date with the minimum possible time
            datetime.datetime.combine(start_date, datetime.time.min),
            # The start_date with the maximum possible time
            datetime.datetime.combine(end_date, datetime.time.max)
        )
    query = query.filter(created__range=start_date_range).values()
    data_dict = ValuesQuerySetToDict(query)
    data_json = json.dumps(data_dict)
    return json_response({'status':data_json})

2 个答案:

答案 0 :(得分:1)

如果要选择相关值,则必须在values()中指定所需的所有参数。否则,您将只获得用户模型的外键。尝试使用__

添加用户模型中所需的值
query = query.filter(created__range=start_date_range).values('approved', ..., 'user_id__daily_target', 'user_id__username')

顺便说一下,如果您要创建API,请查看django-rest-framework

答案 1 :(得分:0)

试试这个,

Deal.objects.filter(user_id__role_id_id=1, created__range=start_date_range).select_related('user_id').values()

或指定必填字段作为值()的参数。