tags:

views:

85

answers:

1

For example:

I have the follow model

class Categories(models.Model):
    name = models.CharField(max_length=100,verbose_name="Category Name")
    parent_cat = models.ForeignKey('self',verbose_name="Parent Category",null=True,blank=True,related_name="child_cat")
    description = models.TextField(verbose_name="Category Description",blank=True)

As one can see, this is a tree-structure table. I also have a ModelForm which consist of a ForeignKey for Categories:

p_category = models.ForeignKey(Categories,verbose_name="Category")

A sample category tree like structure might be as the following:

  • Brand
    • Red
  • Color
    • Red

Each of them have a row in Categories. However you would noticed 2 distinct "Red" rows, both which represent different things, 1 of a red color, the other of a brand named "Red".

However in the ForeignKey modelform, which is represented by the tag in the form, it would show 2 similar "Red" options. This is where I hope to change the verbose value of the tag to reflect something more relevant.

From:

<option>Red</option>

To:

<option>Color > Red</option>

How can I do this?

A: 

I'm not sure if this is the best way, but you could edit the Categories model so it looks like this:

class Categories(models.Model):
    name = models.CharField(max_length=100,verbose_name="Category Name")
    parent_cat = models.ForeignKey('self',verbose_name="Parent Category",null=True,blank=True,related_name="child_cat")
    description = models.TextField(verbose_name="Category Description",blank=True)

    def __unicode__(self):
        name = ''
        if self.parent_cat:
            name = self.parent_cat + ' &gt; '
        return name + self.name

That should give you what you expect.

Paolo Bergantino