开发者

Django foreign keys

开发者 https://www.devze.com 2022-12-19 07:06 出处:网络
I am new to Django and I have the following relation in the model class Name_Mapping(models.Model): AccessionID = models.ForeignKey((\'Feature\', \'Protein\', \'Substrate\'), primary_key = True)

I am new to Django and I have the following relation in the model

class Name_Mapping(models.Model):
 AccessionID = models.ForeignKey(('Feature', 'Protein', 'Substrate'), primary_key = True)
 Element_Name = models.CharField(max_length = 40)

This raises an error:

AssertionError: ForeignKey(('Feature', 'Protein', 'Substrate')) is invalid. First parameter to ForeignKey must be either a model, a model name, or the string 'self'

Can you help me开发者_JAVA百科 with the syntax please?


The syntax for what? You haven't explained what you are trying to accomplish with this. The code only shows us what you tried, and as you know, it didn't work. What was this supposed to do? It looks like perhaps you want a many to many type relationship between Feature, Protein, and Substrate models. (Do you have those models as well? It's not clear.)

If so, you will need to create a separate foreign key for each relationship, let django create its own primary key, and then define a unique together constraint over your foreign keys. Your table will have an extra column to represent the primary key, but that's something you'll just have to accept working with django, as it doesn't allow multiple-column primary keys yet.

Edit: After your comment, it looks like what you are looking for is a generic foreign key. This is the best way to use one relationship to link to multiple other tables.


I agree with jcd. You probably need to use a generic foreign key.

Since you were asking about syntax, you may want to change your class and variable naming conventions to match up with the style you will find in most Python code. See PEP8 for more details but the two big items are to use CapWords for class names and lowercase (with sparing use of underscores for readability) for variables.

Here is a possible rewritten NameMapping model using a generic foreign key and a more PEP8 naming convention:

from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic

class NameMapping(models.Model):
    name = models.CharField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    accession_object = generic.GenericForeignKey('content_type', 'object_id')


Have a look many-to-one-relationships and ForeignKey.

I think you have a wrong idea about what ForeignKey is doing. But without explaining what you want to achieve, it is hard to help.

From your code I would assume you want that AccessionID hold one of the values 'Feature', 'Protein', 'Substrate'. But when you mark it as primary_key it means that you will have max three rows in your table as the primary key is unique.

Update:

After reading other comments, yes you probably have to use Generic Relations. You cannot have one ForeignKey pointing to different tables like you tried.

0

精彩评论

暂无评论...
验证码 换一张
取 消