1

My models look like this:

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)

I need to get device, user, is_alerts_enabled, is_admin, is_owner from UserDevice and first_name, last_name, token from userProfile. This is what I have so far and it gives me what I need from userdevice but I can't figure out how to add the userprofile stuff.

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)

Any help would be greatly appreciated. Thanks!

Mauricio Cortazar
  • 4,049
  • 2
  • 17
  • 27
robert woods
  • 375
  • 2
  • 7
  • 20
  • Possible duplicate of [Convert Django Model object to dict with all of the fields intact](https://stackoverflow.com/questions/21925671/convert-django-model-object-to-dict-with-all-of-the-fields-intact) – solarissmoke Oct 17 '17 at 03:16
  • No this is not a duplicate. – robert woods Oct 17 '17 at 13:07
  • The only connection between those models is a third model: `User`. Are you sure you do not want to add a ForeignKey between `UserDevice` and `UserProfile`? – raratiru Oct 17 '17 at 16:59

1 Answers1

1

The best way to get a dictionary of values is to use values().

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

The above line will give you the relevant fields of the UserDevice model as dictionary values.

If you add a foreign key to UserDevice (here I have changed the user field):

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)

You may do this:

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')

I have used select_related() to avoid hitting the database unnecessarily.

raratiru
  • 8,748
  • 4
  • 73
  • 113