简体   繁体   中英

Unable to autopopulate a Django admin field using an attribute from a OneToOne relationship

There are similar questions to this, but I believe mine is different. I am very new to Django and to Python, so please forgive my ignorance.

I have a custom class UserProfile that inherits from the django.contrib.auth.models User class. This UserProfile is based on the exercise in Tango with Django, however, I am using the example to create a different project/app.

I have UserProfile linked to the standard User model with a OneToOneField relationship in my models.py, as shown below:

class UserProfile(models.Model):
    # Links UserProfile to a User model instance.
    user = models.OneToOneField(User)
    # The additional attribute I wish to include that isn't in User.
    slug = models.SlugField(unique=True)

In my admin.py file, I want an interface for UserProfile that I can work with, and I want the slugfield to autopopulate when I enter a new UserProfile. I want it to autopopulate based on the username attribute of User. However, I can't seem to make it work. Here is my admin.py code:

class UserProfileAdmin(admin.ModelAdmin):
    prepopulated_fields = {"slug": ("user.username",)}

When I try to runserver from my command line, I get the following error:

ERRORS: <class 'climbcast.admin.UserProfileAdmin'>: (admin.E030) The
value of >'prepopula ted_fields["slug"][0]' refers to 'user.username',
which is not an attribute of >' climbcast.UserProfile'.

System check identified 1 issue (0 silenced).

It won't allow me to access the user.username attribute this way, even though I can access it that way in the python shell. Any ideas on how to make this work?

Unfortunately prepopulated_fields doesn't accept DateTimeField , ForeignKey , nor ManyToManyField fields.

Source: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields

Possible solution, in your models.py (make slug attribute optional):

from django.utils.encoding import force_text
from django.template.defaultfilters import slugify


class UserProfile(models.Model):
    [...]
    slug = models.SlugField(blank=True, db_index=True, unique=True)

    def get_unique_slug(self, value):
        """
        Generate a valid slug for a for given string.
        """
        qs = self.__class__.objects.all()
        used = qs.values_list('slug', flat=True)
        i = 1
        baseslug = slugify(value)
        while slug in used:
            slug = '%s-%s' % (baseslug, i)
            i += 1
        return slug

    def save(self, *args, **kwargs):
        if not self.slug and self.user:
          self.slug = self.get_unique_slug(force_text(self.user.username))
        super(UserProfile, self).save(*args, **kwargs)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM