0

I'm adding a new feature to the existing application. This new feature will inform the user how to get an award by collecting emblems. The application will show how to get the award (I plan to use the Django-mptt or probably the Django-treebeard?).

For example, to get Award A, the user needs to collect emblems as follows:

— Award A:
— Emblem AA
— Emblem BB:
— Emblem CC
— Emblem DD

Each emblem will be unique.

Below is the model from an existing application.

class EmblemClass(models.Model):  
    user = models.ForeignKey(User, blank=False, null=False, on_delete=models.CASCADE, related_name=“emblemclasses”)   
    name = models.CharField(max_length=255)   
    image = models.FileField(upload_to=‘images’, blank=True)  
    description = models.TextField(blank=True, null=True, default=None)

The following is the model I'm about to make:

class AwardInfoClass(models.Model):  
    user = models.ForeignKey(User, blank=False, null=False, on_delete=models.CASCADE, related_name=“awardinfoclasses”)  
    name = models.CharField(max_length=255)   
    description = models.TextField(blank=True, null=True, default=None)

class AwardAlignmentClass(models.Model):   
    awardinfoclass = models.ForeignKey(AwardInfoClass, on_delete=models.CASCADE)   
    award_name = models.ForeignKey(Emblem, on_delete=models.CASCADE)   
    award_parent = models.ForeignKey(Emblem, related_name='emblemclasses', blank=True, null=True, on_delete=models.CASCADE)   

In the documentation from the Django-mptt, the 'award_parent' is set to 'self' and not from a different model. I'm using an adjacency list for designing the tree and extending the existing application. Am I making the wrong model design?

I have tried manually without the Django-mptt library. However, I have a problem with editing the child and making a view. I think it will be easier to use the existing library to create such a kind of information tree.

Any thoughts or suggestions? Thank you.

1 Answers1

0

Will this work try it This code is for

— Emblem AA

— Emblem BB:
  — Emblem CC
   — Emblem DD   

from mptt.models import MPTTModel , TreeForeignKey ,TreeManager


class EmblemClass(models.Model):  
    user = TreeForeignKey('self', on_delete=models.CASCADE,blank=True,null=True, related_name=“emblemclasses”)   
    name = models.CharField(max_length=255)   
    image = models.FileField(upload_to=‘images’, blank=True)  
    description = models.TextField(blank=True, null=True, default=None)
K Kumar
  • 71
  • 1
  • 10