djangophotologue

is system_user() defined in django?


My original question was actually how to add a User foreign key to Photolog type class (that uses Imagekit)

I see an answer to a similar question, but when I tried to implement it, I get global name 'system_user' is not defined

I'm not surprised by that, but I am surprised that though it's in an answer, I can't find a reference to system_user in django docs.

(It's not on docs.djangoproject.com, and Google for django+system_user returns nothing interesting.)

I have this in the class Photo in Photologue models.py

def save(self, *args, **kwargs):
    if self.title_slug is None:
        self.title_slug = slugify(self.title)
    if 'owner' not in self.__dict__:
         self.owner = system_user()               # this line fails
    super(Photo, self).save(*args, **kwargs)

How should I import system_user(), or what can I use here instead?


Solution

  • No, system_user is not a django function. You should take all code snippets as pseudo code -- he's just saying "a function that returns my object".

    grep -ri "system_user" /path/to/django returns nothing, so it doesn't exist in the django source.

    Check out the accepted answer in the question you linked to, he overrides the save method, passes in the user object, and manually associates the object to the user.

    In your case, since you're using a model, you'd have to pass in the user object to the model save() method.

    # models
        def save(self, user=None, *args, **kwargs):
            if self.title_slug is None:
                self.title_slug = slugify(self.title)
            if user:
                self.owner = user
            super(Photo, self).save(*args, **kwargs)
    
    # usage in view
    myobj.save(user=request.user)