using an attribute from a foreignkey in an auotslugfield populatefrom() in django

Go To StackoverFlow.com

0

I am trying to do this

    nvc = models.ForeignKey(Nvc)
    slug = AutoSlugField(max_length=50, unique=True, populate_from=('channel_index','nvc__mac_address'))
    channel_index = models.IntegerField()    
    ...

where nvc is a foreignkey with an field mac_address and channel index is a local field

My attempt is based on what is shown to work with "unique_with" in the AutoSlugField (autoslugfield)

# minimum date granularity is shifted from day to month
slug = AutoSlugField(populate_from='title', unique_with='pub_date__month')

But I get this error

'NvcChannel' object has no attribute 'nvc__mac_address'

is it possible to do what i am trying to do? If so, where did i go wrong?

I looked at this question override save to execute code and came up with this

def save(self, *args, **kwargs):
    if not self.pk:
        self.slug = AutoSlugField(max_length=50, unique=True, populate_from=('channel_index',self.nvc.mac_address))
    super(NvcChannel, self).save(*args, **kwargs)
2012-04-05 01:07
by michael


1

nvc__mac_address is only for database lookups (usually with filter()). You are trying to access a field of retrieved object, so you should use nvcchannel.nvc.mac_address

2012-04-05 04:17
by ilvar
so can I do a db look up of the foreign key inside the model definition - michael 2012-04-05 18:26
Ads