Modeform Foreign key column not working

30 views
Skip to first unread message

Aruna Priya

unread,
Oct 20, 2021, 9:17:44 AM10/20/21
to Django users
Hi,

I am trying to create a form from model and the model has a foreign key and am not able to populate the values from the column it refers to.

My form,

class ConnectionForm(forms.ModelForm):
 connection_type = forms.ModelChoiceField(queryset=ConnectionTypes.objects.all(), to_field_name='connection_type_id')

    class Meta:
        model = ConnectionDetails
        exclude = ['connection_id','last_update_date']


lalit suthar

unread,
Oct 20, 2021, 11:07:04 AM10/20/21
to Django users
is `ConnectionTypes` a foreign key to `ConnectionDetails`?
in that case you don't need to put it as a field in form, it will be added automatically in the form. 

Aruna Priya Nagarajan

unread,
Oct 21, 2021, 10:27:15 AM10/21/21
to Django users

yeah, it is a foreign key and I have put integer field instead of foreign key in the model. I changed it now and its working by itself without specifying ModelChoiceField. 

Thanks for your help!! 

Lalit Suthar

unread,
Oct 23, 2021, 1:53:38 PM10/23/21
to django...@googlegroups.com
cool! mention not :)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ddb8c862-57fc-4bb0-97a0-f5c235b00a1fn%40googlegroups.com.

Aruna Priya Nagarajan

unread,
Oct 26, 2021, 9:45:39 AM10/26/21
to Django users
Hi,

I am trying to save the model form and getting error that foreign key column is not there. 

Error:
column "connection_type_id" of relation "connection_details_test" does not exist ( There is no column connection_type_id, why its looking for this column, should I change my model?)

Models:
======

class ConnectionTypes(models.Model):
      connection_type_id models.IntegerField(primary_key=True,default=re_sequence('connection_type_seq'))
      connection_type_name = models.CharField(max_length=100)
      connection_type_desc = models.CharField(max_length=300)
      connection_type_category = models.CharField(max_length=100)
      last_update_date = models.DateTimeField(default=dbcurr_ts)

      class Meta: 
            managed = False
            db_table ='connection_types_test'
            verbose_name = 'connection_types_test'
            verbose_name_plural = 'connection_types_test'

      def __str__(self):
        return str(self.connection_type_id)
    


class ConnectionDetails(models.Model):
      connection_id = models.IntegerField(primary_key=True,default=re_sequence('connection_seq'))
      connection_name = models.CharField(max_length=200)
      connection_type = models.ForeignKey(ConnectionTypes, on_delete=models.CASCADE)
      endpoint = models.CharField(max_length=100)
      port = models.IntegerField()
      login_id = models.CharField(max_length=100)
      login_password = fields.CharPGPPublicKeyField(max_length=100)
      connection_string_1 = fields.CharPGPPublicKeyField(max_length=100)
      connection_string_2 = fields.CharPGPPublicKeyField(max_length=100)
      connection_string_3 = fields.CharPGPPublicKeyField(max_length=100)
      aws_region = models.CharField(max_length=20)
      owner_id = models.IntegerField()
      last_update_date = models.DateTimeField(default=dbcurr_ts)
      working_schema = models.CharField(max_length=100)
      service = models.CharField(max_length=100)

      def generate_enc(mystrenc):
          return 'pass'

      class Meta:
            managed = False
            db_table = 'connection_details_test'
            verbose_name = 'connection_details_test'
            verbose_name_plural = 'connection_details_test'

Model Form :
==========

class ConnectionForm(forms.ModelForm):

    login_password = forms.CharField(widget=forms.PasswordInput())
    owner_id = forms.IntegerField(widget=forms.HiddenInput(), required=False)
    
    class Meta:
        model = ConnectionDetails
        exclude = ['connection_id','last_update_date']


View.py
======
def addconnection(request):
    connectionform = ConnectionForm(request.POST or None)
    if connectionform.is_valid():
        ownerqs = AuthUser.objects.values('id').get(username = request.user.username)
        connectionform.cleaned_data['owner_id'] = int(ownerqs['id'])
        connectionform.save()
        messages.success(request, f"sucess!")
    else:
        messages.success(request, f"Failure!")
    return render(request,'main/ssaddconnection.html',{"cform":
    connectionform,"CanUseMod":UserModules.objects.filter(user_id=request.user.id).filter(module_id=1).count(),"UserIsAuth":request.user.is_authenticated})
    
Anyone can help me on this??

Thanks,
Aruna 
Reply all
Reply to author
Forward
0 new messages