OdifYltsaeb / django-multilingual

Automatically exported from code.google.com/p/django-multilingual
MIT License
0 stars 0 forks source link

fk_name 'master' is not a ForeignKey to #104

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
1. Steps - setup mulilinugal and create the next model (provided below)
You can create TuneFile from admin, but when you try to add TuneCommenter 
or TuneComment you get this error:

Exception at /admin/tunefiles/tunecommenter/add/
fk_name 'master' is not a ForeignKey to <class 
'tunefiles.models.TuneCommenter'>

from django.db import models
import multilingual

class TuneFile(models.Model):
    code = models.SlugField(max_length=50, primary_key=True)
    file = models.FileField(upload_to='tune_files')
    picture = models.ImageField(upload_to='tune_imgs')
    rating = models.PositiveSmallIntegerField()
    active = models.BooleanField()
    created_at = models.DateTimeField(auto_now_add=True)
    class Translation(multilingual.Translation):
        name = models.CharField(max_length=255)
        description = models.TextField()
    def __unicode__(self):
        return self.name

class TuneCommenter(models.Model):
    nick = models.CharField(max_length=50, primary_key=True)
    avatar = models.ImageField(upload_to='avatars')
    created_at = models.DateTimeField(auto_now_add=True)
    active = models.BooleanField()
    class Translation(multilingual.Translation):
        desc = models.CharField(max_length=100)
    def __unicode__(self):
        return self.nick

class TuneComment(models.Model):
    body = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    commenter = models.ForeignKey(TuneCommenter)
    tune_file = models.ForeignKey(TuneFile)
    active = models.BooleanField()
    class Translation(multilingual.Translation):
        desc = models.CharField(max_length=100)
    def __unicode__(self):
        return u'%s : %s' % (self.commenter.nick, self.body)

The latest version of Django 1.1 and Trunk version of django-multilinugal 
is used.

Original issue reported on code.google.com by Person...@gmail.com on 23 Aug 2009 at 5:57

GoogleCodeExporter commented 8 years ago
/usr/lib/python2.6/site-packages/django/forms/models.py in _get_foreign_key

 795. if fk_name:
 796. fks_to_parent = [f for f in opts.fields if f.name == fk_name]
 797. if len(fks_to_parent) == 1:
 798. fk = fks_to_parent[0]
 799. if not isinstance(fk, ForeignKey) or \
 800. (fk.rel.to != parent_model and
 801. fk.rel.to not in parent_model._meta.get_parent_list()):

 ***802. raise Exception("fk_name '%s' is not a ForeignKey to %s" % (fk_name, 
parent_model)) ...

 803. elif len(fks_to_parent) == 0:
 804. raise Exception("%s has no field named '%s'" % (model, fk_name))
 805. else:
 806. # Try to discover what the ForeignKey from model to parent_model is
 807. fks_to_parent = [
 808. f for f in opts.fields

▼ Local vars
Variable    Value
ForeignKey  
<class 'django.db.models.fields.related.ForeignKey'>
can_fail    
False
f   
<multilingual.fields.TranslationForeignKey object at 0x9b7ba0c>
fk  
<multilingual.fields.TranslationForeignKey object at 0x9b7ba0c>
fk_name     
'master'
fks_to_parent   
[<multilingual.fields.TranslationForeignKey object at 0x9b7ba0c>]
model   
<class 'tunefiles.models.TuneFileTranslation'>
opts    
<Options for TuneFileTranslation>
parent_model    
<class 'tunefiles.models.TuneCommenter'>

Original comment by Person...@gmail.com on 23 Aug 2009 at 6:12

GoogleCodeExporter commented 8 years ago
SQLite is used.

[wiz@archy dtunes]$ python manage.py sqlall tunefiles
BEGIN;
CREATE TABLE "tunefiles_tunefile_translation" (
    "id" integer NOT NULL PRIMARY KEY,
    "name" varchar(255) NOT NULL,
    "description" text NOT NULL,
    "language_id" integer NOT NULL,
    "master_id" varchar(50) NOT NULL,
    UNIQUE ("language_id", "master_id")
)
;
CREATE TABLE "tunefiles_tunefile" (
    "code" varchar(50) NOT NULL PRIMARY KEY,
    "file" varchar(100) NOT NULL,
    "picture" varchar(100) NOT NULL,
    "rating" smallint unsigned NOT NULL,
    "active" bool NOT NULL,
    "created_at" datetime NOT NULL
)
;
CREATE TABLE "tunefiles_tunecommenter_translation" (
    "id" integer NOT NULL PRIMARY KEY,
    "desc" varchar(100) NOT NULL,
    "language_id" integer NOT NULL,
    "master_id" varchar(50) NOT NULL,
    UNIQUE ("language_id", "master_id")
)
;
CREATE TABLE "tunefiles_tunecommenter" (
    "nick" varchar(50) NOT NULL PRIMARY KEY,
    "avatar" varchar(100) NOT NULL,
    "created_at" datetime NOT NULL,
    "active" bool NOT NULL
)
;
CREATE TABLE "tunefiles_tunecomment_translation" (
    "id" integer NOT NULL PRIMARY KEY,
    "desc" varchar(100) NOT NULL,
    "language_id" integer NOT NULL,
    "master_id" integer NOT NULL,
    UNIQUE ("language_id", "master_id")
)
;
CREATE TABLE "tunefiles_tunecomment" (
    "code" smallint unsigned NOT NULL PRIMARY KEY,
    "body" text NOT NULL,
    "created_at" datetime NOT NULL,
    "commenter_id" varchar(50) NOT NULL REFERENCES 
"tunefiles_tunecommenter" ("nick"),
    "tune_file_id" varchar(50) NOT NULL REFERENCES "tunefiles_tunefile" ("code"),
    "active" bool NOT NULL
)
;
CREATE INDEX "tunefiles_tunefile_translation_language_id" ON 
"tunefiles_tunefile_translation" ("language_id");
CREATE INDEX "tunefiles_tunefile_translation_master_id" ON 
"tunefiles_tunefile_translation" ("master_id");
CREATE INDEX "tunefiles_tunecommenter_translation_language_id" ON 
"tunefiles_tunecommenter_translation" ("language_id");
CREATE INDEX "tunefiles_tunecommenter_translation_master_id" ON 
"tunefiles_tunecommenter_translation" ("master_id");
CREATE INDEX "tunefiles_tunecomment_translation_language_id" ON 
"tunefiles_tunecomment_translation" ("language_id");
CREATE INDEX "tunefiles_tunecomment_translation_master_id" ON 
"tunefiles_tunecomment_translation" ("master_id");
CREATE INDEX "tunefiles_tunecomment_commenter_id" ON 
"tunefiles_tunecomment" ("commenter_id");
CREATE INDEX "tunefiles_tunecomment_tune_file_id" ON 
"tunefiles_tunecomment" ("tune_file_id");
COMMIT;

Original comment by Person...@gmail.com on 23 Aug 2009 at 6:28