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)
Community
  • 1
  • 1
michael
  • 2,577
  • 5
  • 39
  • 62

1 Answers1

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

ilvar
  • 5,718
  • 1
  • 20
  • 17