0

I'm looking to assign a default value using a method inside model:

class Discussion(models.Model):
    # attributes ...

    def __str__(self):
            return str(self.id) + ". " + self.title

    def class_name(self):
        return self.__class__.__name__

    discussion_type = models.CharField(max_length = 50, default = self.class_name())

class TechDiscussion(Discussion):
    # ...

class ScienceDiscussion(Discussion):
    # ...

In my Django app, users can only create science or tech discussions. Thus discussion_type should be either "TechDiscussion" or "ScienceDiscussion".

Server returns error NameError: name 'self' is not defined, referring to the default value assigned to discussion_type.

2
  • 1
    simple solution is override the save method of the Super model. Commented Aug 28, 2018 at 15:01
  • @BearBrown thanks it worked. This seems to be a standard, documented way of adding functionality upon saving a model instance. Commented Aug 28, 2018 at 16:24

2 Answers 2

1

As Bear Brown suggested in the comment a solution would be to override the save() method of the Discussion class, also documented here. I removed the discussion_type assignment and added the override, as in the documentation:

class Discussion(models.Model):
    # attributes ...

    def __str__(self):
            return str(self.id) + ". " + self.title

    def class_name(self):
        return self.__class__.__name__

    def save(self, *args, **kwargs):
            self.call_type = self.class_name()
            super().save(*args, **kwargs)
Sign up to request clarification or add additional context in comments.

1 Comment

if not self.call_type: other way you have not default but constant.
0

This cannot work as the Discussion model will have its own database table - so you need to make it abstract. It will still try to access self where there is no object, so replace this with the class name. And it shall not evaluate the function when assigning just when saving so just add the function object there (without brackets).

class Discussion(models.Model):
    # attributes ...

    def __str__(self):
            return str(self.id) + ". " + self.title

    def class_name(self):
        return self.__class__.__name__

   discussion_type = models.CharField(max_length = 50, default = Discussion.class_name)

   class Meta:
       abstract = True

However, I have not tested this if this really works.

1 Comment

Now I get the error: NameError: name 'Discussion' is not defined

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.