Django model with a list
<p>How to implement a list in a Django model?</p>

<p>Lets say I have a <code>UserProfile</code> model, and each user can have a list of mortgages (undefined quantity) defined by <code>MortgageModel</code>.</p>

<pre><code>class MortgageModel(models.Model):
   bank_name = models.CharField(max_length=128)
   sum = models.BigIntegerField()

class UserProfile(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE)
   # list of morgtages?
</code></pre>

<p>my only idea is to make a old school list, where every mortgage can point to another one or null, like this:</p>

<pre><code>class MortgageModel(models.Model):
   bank_name = models.CharField(max_length=128)
   sum = models.BigIntegerField()
   next_mortgage = MortgageModel(null=True, default=null)

class UserProfile(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE)
   mortgages = MortgageModel(null=True, default=null)
</code></pre>

<p>is there any other possibility?</p>

<p>You'll have to assign a <code>ForeignKey</code> to <code>User</code> , so that each <code>Mortgate</code> 'belongs' to a user. 
That is how a <strong><em>One-To-Many relationship</em></strong> is done. Then, if you want to get the list of Mortgages a user have, you'd filter them out like <code>MortgageModel.objects.filter(related_user=user)</code></p>

<p>So, you'd have something like</p>

<h3>Model</h3>

<pre><code>   class MortgageModel(models.Model):
       bank_name = models.CharField(max_length=128)
       sum = models.BigIntegerField()
       related_user = models.ForeignKey(UserProfile)

    class UserProfile(models.Model):
       user = models.OneToOneField(User, on_delete=models.CASCADE)
</code></pre>

<h3>View</h3>

<pre><code>list_of_mortages = MortgageModel.objects.filter(related_user=user)
</code></pre>

python django