Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django models definition ordering

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?

like image 997
artem Avatar asked Dec 19 '11 19:12

artem


People also ask

What is ordering in Django 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")

What is ordering in Meta class?

class Meta: verbose_name = "stu" # add verbose_name here. 4. ordering. Ordering is basically used to change the order of your model fields.

What are the Django models?

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.

What is def __ str __( self in Django?

def __str__( self ): return "something" This will display the objects as something always in the admin interface.


1 Answers

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):
    # ...
like image 182
jdi Avatar answered Sep 21 '22 05:09

jdi