[Answered ]-Update Table Relationship in Django Admin

2๐Ÿ‘

โœ…

I would slightly change the models to accomodate ManyToManyFields like this:

class Sites(models.Model):
    name = models.CharField(max_length=75)
    link = models.CharField(max_length=150)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.name

class PaymentProcessors(models.Model):
    name = models.CharField(max_length=75)
    sites = models.ManyToManyField('Sites', related_name='payment_processors')

    def __str__(self):
        return self.name

Now, if you want custom fields or store more information along with the relationship, you can make use of the through table

For example, if you want to associate the amount limit or something more custom:

class Sites(models.Model):
    name = models.CharField(max_length=75)
    link = models.CharField(max_length=150)
    description = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.name

class PaymentProcessors(models.Model):
    name = models.CharField(max_length=75)
    sites = models.ManyToManyField('Sites', related_name='payment_processors', through='SitePaymentProcessor')

    def __str__(self):
        return self.name

from django.core.validators import MaxValueValidator
class SitePaymentProcessor(models.Model):
    site = models.ForeignKey('Site')
    payment_processors = models.ForeignKey('PaymentProcessors')
    amount_limit = models.IntegerField(default=1000,
        validators=[
            MaxValueValidator(100)
        ])

Now, again this is just an example.

Now, registering the admin classes would enable you to populate data into the models via the admin interface.

To auto-populate a large dataset, I would consider using fixtures rather than populating elements individually.

๐Ÿ‘คkarthikr

Leave a comment