У меня две модели, хочу получить значение одной модели от другой. Все это объясняется в комментариях view.py.
Ошибка:
Объект «Service» не имеет атрибута «business_profile»
models.py
class Service(models.Model):
name = models.CharField(max_length=50)
image = models.ImageField(upload_to='image', blank = True)
#business_profile = models.ManyToManyField("BusinessProfile", blank=True, related_name="business_of_services")
def __str__(self):
return "Service - {}".format(self.name)
class BusinessProfile(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
business_name = models.CharField(max_length=100, unique =True)
register_date = models.DateTimeField(default=timezone.now)
pan_number = models.IntegerField(unique=True)
pan_name = models.CharField(max_length=100)
address = models.TextField(max_length=200)
pincode = models.IntegerField()
city = models.CharField(max_length=50)
state = models.CharField(max_length=50)
service = models.ManyToManyField("Service", blank=True, related_name="services_of_business")
image = models.ManyToManyField("Service", related_name="image_of_business")
Views.py
service = Service.objects.all() #getting all the services
for i in service :
print(i.name) # working
# Now as services is also connected to businessprofile I want to print business_id for this service.
print(i.business_id) # something like this