title: Filtering data in real time in django forms
body: <p>I have two problems strictly. The first is the problem with the field type in forms.py. Because I'm trying to use a foreign key as a value in the check box and I have the error that the "int () must be a string, a byte-like object or a number, not" ModelChoiceField "and I don't know what to do with it. The second main problem is data filtering in the interface in real time. What I mean? I have a user model like:</p>
  
  <pre class="lang-py prettyprint-override"><code># user/models.py:
  class Profile(models.Model):
      user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
      country= models.ForeignKey(Country, on_delete=models.SET_NULL, null=True)
      city = models.ForeignKey(City, on_delete=models.SET_NULL, null=True)
      year = models.IntegerField(choices=YEARS, default=1)
      image = models.ImageField(default='default.jpg', upload_to='profile_pics')
  </code></pre>
  
  <p>And in forms I want to see only those cities that are in the selected country. For example, we have record like:</p>
  
  <pre><code>London, United Kingdom;
  York, United Kingdom; 
  Berlin, Germany;
  </code></pre>
  
  <p>and if the user chooses Germany, he should only see Berlin in the area with the cities.
  I hope you know what I want to achieve and someone will be able to help me.</p>
  
  <pre class="lang-py prettyprint-override"><code># forms.py:
  
  class ProfileUpdateForm(forms.ModelForm):
      country =forms.ModelChoiceField(queryset=Country.objects.all())
      city = forms.ModelChoiceField(queryset=City.objects.filter(country=country))
  
      class Meta:
          model = Profile
          fields = ['website','country', 'city', 'year', 'image']
  
  
  # city/models.py
  
  class Country(models.Model):
      name = models.CharField(max_length=100)
  
      def __str__(self):
          return self.name
  
  
  class City(models.Model):
      name = models.CharField(max_length=100)
      country = models.ForeignKey(Country, on_delete=models.CASCADE)
      code = models.CharField(max_length=7)
  
      def __str__(self):
          return self.name
  
  
  </code></pre>
  
  <p>EDIT:
  I think you need this piece of code:</p>
  
  <pre class="lang-py prettyprint-override"><code>@login_required
  def profile(request):
  
      if request.method == 'POST':
          u_form = UserUpdateForm(request.POST, instance=request.user)
          p_form = ProfileUpdateForm(request.POST,
                                     request.FILES,
                                     instance=request.user.profile)
          if u_form.is_valid() and p_form.is_valid():
              u_form.save()
              p_form.save()
              messages.success(request, 'Twoje dane zostały uaktualnione!')
      else:
          u_form = UserUpdateForm(instance=request.user)
          p_form = ProfileUpdateForm(instance=request.user.profile)
  
      context = {
          'u_form': u_form,
          'p_form': p_form,
      }
  
      return render(request, 'users/profile.html', context)
  </code></pre>