2017-10-16 153 views
1

我的模式是這樣的:從兩個Django模型獲取數據

class UserDevice(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=False) 
    device = models.ForeignKey(Device, on_delete=models.PROTECT, null=False) 
    activation_date = models.DateTimeField(default=timezone.now, null=False) 
    friendly_name = models.CharField(max_length=20, null=True, blank=True) 
    is_owner = models.BooleanField(null=False, default=False) 
    is_admin = models.BooleanField(null=False, default=True) 
    is_alerts_enabled = models.BooleanField(null=False, default=True) 
    timeStamp = models.DateTimeField(auto_now = True, null=False) 

class UserProfile(models.Model): 
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.PROTECT, null=False) 
    token = models.TextField(null=False, blank=True) 
    first_name = models.TextField(null=True, blank=True) 
    last_name = models.TextField(null=True, blank=True) 
    timeStamp = models.DateTimeField(auto_now = True, null=False) 

我需要從userProfileUserDevicefirst_name, last_name, token得到device, user, is_alerts_enabled, is_admin, is_owner。這是我迄今爲止,它給了我我需要從userdevice,但我不知道如何添加userprofile東西。

nonOwners = UserDevice.objects.filter(device=device, is_owner=False) 

if nonOwners is None: 
    return errormsg('non nonOwners found for this device') 

nonOwnersArray=[] 

for nonOwner in nonOwners: 

    nonOwner_data = model_to_dict(nonOwner, 
           fieldlist=(
            'device.serial', 
            'user.email', 
            'is_alerts_enabled', 
            'is_admin', 
            'is_owner', 
            ), 
           rename={ 
            'device.serial': 'serial', 
            'user.email': 'email'}) 


    nonOwnersArray.append(nonOwner_data) 

任何幫助將不勝感激。謝謝!

+0

[將Django模型對象轉換爲所有字段不變的可能的重複](https://stackoverflow.com/questions/21925671/convert-django-model-object-to-dict-with-all-of -the-fields-intact) – solarissmoke

+0

不,這不是重複的。 –

+0

這些模型之間的唯一聯繫是第三個模型:'用戶'。你確定你不想在'UserDevice'和'UserProfile'之間添加一個ForeignKey嗎? – raratiru

回答

1

獲取值的字典的最好方法是使用values()

UserDevice.objects.filter(device=device, is_owner=False).values('device', 'user', 'is_alerts_enabled', 'is_admin', 'is_owner') 

上面的代碼將爲您提供UserDevice模型的相關字段作爲字典值。

如果添加一個外鍵UserDevice(這裏我已經改變了user場):

class UserDevice(models.Model): 
    user = models.ForeignKey(UserProfile, on_delete=models.PROTECT, null=False) 
    device = models.ForeignKey(Device, on_delete=models.PROTECT, null=False) 
    activation_date = models.DateTimeField(default=timezone.now, null=False) 
    friendly_name = models.CharField(max_length=20, null=True, blank=True) 
    is_owner = models.BooleanField(null=False, default=False) 
    is_admin = models.BooleanField(null=False, default=True) 
    is_alerts_enabled = models.BooleanField(null=False, default=True) 
    timeStamp = models.DateTimeField(auto_now = True, null=False) 

你可以這樣做:

UserDevice.objects.select_related('user').filter(device=device, is_owner=False).values('device', 'user', 'is_alerts_enabled', 'is_admin', 'is_owner', 'user__first_name', 'user__last_name', 'user__token') 

我已經使用select_related(),以避免擊中數據庫不必要的。