Jamshy
Jamshy

Reputation: 70

Django error. Cannot assign must be an instance while insert into table

While insert into one of my table i got following error message:

Cannot assign "1": "Country.intModifiedUser" must be a "AppUser" instance.

country_details.intModifiedUser = 1

views.py

country_details = Country.objects.get(pk=pk,chrDocumentStatus='N')
country_details.intModifiedUser = 1
country_details.save()

model.py

class Country(UpdateLog):
intCountryId = models.BigAutoField(primary_key = True, db_column = 'pk_bint_country_id')
strCountryName = models.CharField('Country Name', db_column = 'vchr_country', max_length = 100, null = False)
strNationality = models.CharField('Nationality', db_column = 'vchr_nationality', max_length = 100, null = True)
chrDocumentStatus = models.CharField('Document Status', db_column = 'chr_document_status', max_length = 1, null = False, default = 'N')
intModifiedUser = models.ForeignKey(AppUser,related_name='%(class)s_modified', db_column= 'fk_bint_modified_user_id', null = True)

class Meta:
    db_table='tbl_country'

class AppUser(AbstractBaseUser,ChangeLog):
   email = models.EmailField(
    verbose_name='email address',
    max_length=255,
    unique=True,
) 

chrUserType = models.CharField('User type code', db_column='chr_user_type',max_length=5,blank=True,null=True)
blnSuperUser = models.BooleanField(default=False, db_column='is_superuser')
intTravelAgencyUserId = models.ForeignKey(TravelAgencyUser,db_column='fk_travel_agency_user_id',default=None)
objects = MyUserManager()

USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
class Meta:
    db_table='tbl_user'
def is_staff(self):
    return True
def has_module_perms(self, re):
    return self.is_superuser
def has_perm(self, re):
    return self.is_superuser

def get_short_name(self):
    return self.email

Upvotes: 0

Views: 1019

Answers (1)

Jay Venkat
Jay Venkat

Reputation: 397

intModifiedUser is an instance of AppUser. You should not set the foreign key identifier directly.

Use to get the object from AppUser Instance

intModifiedUser = AppUser.objects.get(id=1) 

country_details = Country(values, intModifiedUser = intModifiedUser)

Upvotes: 2

Related Questions