For example, I have 2 models: Model1 and Model2. Model1 has field ForeignKey(Model2). Model2 has method, that returns all instances of Model1 which has this instance of Model2 as ForeignKey.
But it doesn't work, because Model2 is defined after Model1 and it knows nothing about Model2. How to solve this problem?
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")
class Meta: verbose_name = "stu" # add verbose_name here. 4. ordering. Ordering is basically used to change the order of your model fields.
Django web applications access and manage data through Python objects referred to as models. Models define the structure of stored data, including the field types and possibly also their maximum size, default values, selection list options, help text for documentation, label text for forms, etc.
def __str__( self ): return "something" This will display the objects as something always in the admin interface.
Take a look at the django docs. You can specify the model using a string so it evaluates later: https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey
class Car(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
# ...
class Manufacturer(models.Model):
# ...
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