Home > Enterprise >  How to get details from two models in django
How to get details from two models in django

Time:05-11

I am trying to build an api endpoint that performs a GET request, path -> {url}/api/v1/users/:id

I have three models

class PvSystem(models.Model):
   modelNo = models.CharField(max_length=10, blank=False, default='')
   modelName = models.CharField(max_length=10, blank=False, default='')
   dimensionPanel: models.FloatField(max_length=10, blank=False, default=NULL)
   elevation = models.FloatField(max_length=10, blank=False, default=NULL)
   kwhPeak = models.FloatField(max_length=10, blank=False, default=NULL)
   avergaeSurplus = models.FloatField(max_length=10, blank=False, default=NULL)

class EnergyData(models.Model):
   pvId = models.ForeignKey(PvSystem,related_name='orders',on_delete=models.CASCADE)
   energyNeeded = models.FloatField(max_length=10, blank=False, default=NULL)
   energyConsumption = models.FloatField(max_length=20, blank=False, default=NULL)
   energyProduction = models.FloatField(max_length=20, blank=False, default=NULL)
   energySurplus = models.FloatField(max_length=20, blank=False, default=NULL)
   floorPrice = models.FloatField(max_length=10, blank=False, default=NULL)
   capPrice = models.FloatField(max_length=10, blank=False, default=NULL)
   pvStatus = models.BooleanField(blank=False, default=False)
   dsoId = models.CharField(max_length=70, blank=False, default='')
   supplier = models.CharField(max_length=70, blank=False, default='')

class User(models.Model):
   name = models.CharField(max_length=70, blank=False, default='')
   email = models.EmailField(max_length=70, blank=False, default='')
   password = models.CharField(max_length=70, blank=False, default='')
   address = models.CharField(max_length=70, blank=False, default='')
   roleId = models.IntegerField(blank=False, default='1')
   isActive = models.BooleanField(blank=False, default=TRUE)
   dsoId = models.CharField(max_length=70, blank=False, default='')
   supplier = models.CharField(max_length=70, blank=False, default='')
   dateJoined = models.DateTimeField(auto_now_add=False, blank=False, default=NULL)

Models Description

UserModel: Registered details of the user. EnergyData: A table that provides more details of the user using the users dsoID and supplier to check. PVSystem: Connected to the Energydata hence return result of a user with energy data and pvstatus TRUE.

From the above, when the api get request call is made, it is supposed to provide a result of user depending on the id, but i want it to show not only contents on the user model but contents on the energy data model(by doing a check using the dsoID and supplier of that user called.)

Please how can i achieve this? I am a newbie to django.

CodePudding user response:

You can try it in this way:

user = User.objects.get(dsoId=id)#I am assuming dsoId as primary key
enrgydata = EnergyData.objects.get(dsoId=user.dsoId) 

If the relation between Users and EnergyData is One-to-One, then you can directly use id from url parameter to query EnergyData table. If it is One-to-Many or Many-To-One, you need to do in above method.

CodePudding user response:

class EnergyDataSerializer(serializers.ModelSerializer):
    def profile_info(self, obj): 
        prof_obj= User.objects.get(dsoId=obj.dsoId)

        # access the fields or serialize the data if you can in other way. I am simply returning it in key:value
        return {'id':prof_obj.id, 'name':prof_obj.name, 'email': prof_obj.email}

    profile = serializers.SerializerMethodField('profile_info')
    class Meta:
            model = EnergyData
            fields ='__all__'
  • Related