[Answer]-Display data from database in django

1👍

If your problem is with the <Types: Types object> part, add a __unicode__() method to your Types model.

class Types(models.Model):
    user = models.ForeignKey(User, null=True)
    title = models.CharField('Incident Type', max_length=200)
    parent_type_id = models.CharField('Parent Type', max_length=100, null=True, blank=True)
    is_active = models.BooleanField('Is Active', default=True)

    def __unicode__(self):
        return self.title

    @property
    def parent_types(self):
        """ For accessing the parent types without introducing relationship """
        return self.objects.filter(pk=self.parent_type_id)

This will show the title of the object in the list instead of <Types: Types object>.

Update: I’ve added a property so that you can access the parent types like types.parent_types without changing the model.

Leave a comment