# models.py
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
class Price(models.Model):
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
service = GenericForeignKey('content_type', 'object_id')
amount = models.DecimalField(max_digits=10, decimal_places=2)
# Add other fields specific to the price
def __str__(self):
return f"Price for {self.service}"
# views.py
# Creating a price for ServiceA
service_a_instance = ServiceA.objects.get(id=1)
price_a = Price.objects.create(service=service_a_instance, amount=19.99)
# Creating a price for ServiceB
service_b_instance = ServiceB.objects.get(id=1)
price_b = Price.objects.create(service=service_b_instance, amount=29.99)
# Accessing prices for all services
all_prices = Price.objects.all()
for price in all_prices:
print(f"Price for {price.service}: {price.amount}")