Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, if I understood what you want. You'll have to modify your model to:</p> <pre><code>#models.py class ModelA(models.Model) field1 = models.CharField(max_length=10) field2 = models.ForeignKey(ModelA) class ModelB(models.Model) field3 = models.CharField(max_length=10) </code></pre> <p>So, you can do:</p> <pre><code>#views.py b = ModelB(field3 = 'goodbye') c = ModelB(field3 = 'goodbye again') a = ModelA(field1 = 'hello', field2 = [b, c]) d = ModelA(field1 = 'another', field2 = a.field2) </code></pre> <p>Or, if you prefer to keep your model you can do:</p> <pre><code>#models.py class ModelA(models.Model) field1 = models.CharField(max_length=10) class ModelB(models.Model) field2 = models.CharField(max_length=10) field3 = models.ForeignKey(ModelA) #views.py a = ModelA(field1 = 'hello') b = ModelB(field2 = 'goodbye', field3 = a) c = ModelB(field2 = 'goodbye again', field3 = a) d = ModelA(field1 = 'another') #here you get all the objects related to "a" query = ModelB.objects.filter(field3=a) #iterated over them for obj in query: obj.field3 = d </code></pre> <p>Another way I think it can be what you want is:</p> <pre><code>#models.py class ModelA(models.Model) field1 = models.CharField(max_length=10) field2 = models.ForeignKey(ModelA) class ModelB(models.Model) field3 = models.CharField(max_length=10) field4 = models.ForeignKey(ModelA) #views.py a = ModelA(field1 = 'hello') b = ModelB(field3 = 'goodbye', field4 = a) c = ModelB(field3 = 'goodbye again', field4 = a) d = ModelA(field1 = 'another', field2 = a) </code></pre> <p>I think it's done. But I don't know, I hope it could be usefull to you. ^^</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload