1

I have a model(profile) which has generic relation with another model(member) in another app. When I want to add a new profile object I have to use object_id which is a field that shows the ID of that member object which has relation with this profile object. I want to use another field instead of that, for example, I have a content_object field that shows exactly the object itself. Is there a way to use this field(content_object ) instead of object_id feild.

profiling.models.Profile:

class Profile(models.Model):
    #objects = ProfileManager()
    MAN = 'M'
    WOMAN = 'W'
    OTHER = 'O'
    GENDER_TYPE = [
        (MAN, 'Man'),
        (WOMAN, 'Woman'),
        (OTHER,'Other'),
    ]
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    email = models.EmailField(unique=True)
    phone = models.CharField(max_length=30)
    birthdate = models.DateField()
    gender = models.CharField(max_length=1,choices=GENDER_TYPE)
    address = models.TextField()
    profile_image = models.ImageField(null=True, blank=True)
    #to define generic relation
    content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
    object_id = models.PositiveIntegerField(unique=True)
    content_object= GenericForeignKey()
    
    def __str__(self) :
        return f'{self.first_name} {self.last_name}' 

ticketing.models.Member:

class Member(models.Model):
    username = models.CharField(max_length=100, unique=True)

    def __str__(self) -> str:
        return self.username
    
    class Meta:
        ordering = ['username']

enter image description here

Waldemar Podsiadło
  • 1,365
  • 2
  • 5
  • 12

0 Answers0