Kervvv
Kervvv

Reputation: 845

Creating a Django Property

I copied this piece of code from another SO question (link).

from django.template.defaultfilters import truncatechars  # or truncatewords

class Foo(models.Model):
    description = models.TextField()

    @property
    def short_description(self):
        return truncatechars(self.description, 100)

class FooAdmin(admin.ModelAdmin):
    list_display = ['short_description']

I also copied this piece of code from this question (link).

class Projects(models.Model):
    Name = models.CharField(max_length=100, null=True, blank=False) 
    Date = models.DateField(null=True, blank=False)
    Month = models.CharField(max_length=100, null=True, blank=False)   

        def get_month(self):
            if self.Date:
                self.Month = self.Date.strftime("%B")
                self.save()

I am aware of the concepts being discussed in these 2 questions, however I am not sure where I can find the list of available options. For example, the use of return truncatechars(self.description, 100) and the use of self.Month = self.Date.strftime("%B"), where is the library where I can find my available options? I want to create a new property but not sure where to find my 'library' of options.

I also assume this will always be made in the models.py?

Upvotes: 0

Views: 94

Answers (1)

doniyor
doniyor

Reputation: 37934

I dont know what you mean by 'library' of options, there is no library for defining a property in Django. It is just a normal attribute of Python since version 2.2.

and yes, models.py is the place where you define it since only there you define the fields of a class.

> example for property from official docs

Upvotes: 2

Related Questions