I am new to Django 1.9, I have an models.py :
class MyProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
Role = ArrayField(models.CharField(max_length=1000), blank=True,null=True)
ContactNumber = models.CharField(max_length=100)
class Meta:
ordering = ('date_joined',)
Here I want to order table using date_joined
which is field of User model
.
When I am trying to run python manage.py makemigrations
it gives me error as follows:
SystemCheckError: System check identified some issues:
ERRORS:
projectmanagement.UniservedTeam: (models.E015) 'ordering' refers to the non-existent field 'date_joined'.
How do i achieve this?
Ordering is basically used to change the order of your model fields.
str function in a django model returns a string that is exactly rendered as the display name of instances for that model.
Django order by lets you specify how your query results should be ordered. Here's an example where we will order by the author's name: class Author(models.Model): name = models.CharField() Author.objects.order_by("name")
Model Inheritance in Django works almost identically to the way normal class inheritance works in python. In this article we will revolve around how to create abstract base class in Django Models. Abstract Base Class are useful when you want to put some common information into a number of other models.
To set ordering on generic api views:
http://www.django-rest-framework.org/api-guide/filtering/#orderingfilter
use: ordering = ('user__date_joined', )
EDIT:
you can specify ordering on meta class like this:
class Meta:
ordering = ('user__date_joined', )
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With