CrazyGeek
CrazyGeek

Reputation: 3437

Is it good practice to save multiple Foreign Keys into CharField as a comma separated values

just want to know whether its good to save multiple Foreign Keys into single Char field to avoid multiple DB calls to save and also maintain OnetoOne Relationship. Here is the situation.

Person Model

class Person(models.Model):
   first_name = models.CharField(max_length=20, null=False, blank=False)
   first_name = models.CharField(max_length=20, null=False, blank=False)
   .....
   .....
   .....  

Secondary Degree Model

class SecondaryDegree(models.Model):
   name = models.CharField(max_length=100, null=False, blank=False)

Person Secondary Degree With OnetoOne Relationship

class PersonSecondaryDegree(models.Model):
   nomination_profile = models.OneToOneField('Person',null=False, blank=False)

   # Here i am using CharField instead of ForiegnKey.
   secondary_degree = models.CharField(max_length=255, null=False, blank=False) 

   is_deleted = models.BooleanField(default=False)
   last_modified = models.DateTimeField(auto_now_add=True,null=False,blank=False)

Upvotes: 0

Views: 317

Answers (1)

gitaarik
gitaarik

Reputation: 46350

I wouldn't advice to use a CharField to store Foreign Key id's. It will prevent you from using standard SQL queries for simple operations, thus will prevent you from using the related features in the Django ORM.

It's usually best to give your database a logical structure and not make structure sacrifices for the performance. There are many other ways to improve performance, like indexing and caching. If you do it like that it will give you the most flexibility for the future.

Upvotes: 2

Related Questions