Adding models from one app to another app

22 views
Skip to first unread message

Inderpreet Singh

unread,
May 28, 2014, 1:31:30 PM5/28/14
to django...@googlegroups.com
This is directory structure of my django site
---- LibreHatti
   |----- src
      |----- useraccounts
      |----- librehatti
         |------- catalog
         |------- reports

Now what we need is to reference a model from useraccounts in librehatti.catalog as a foreign key and I am unable to do that.

I tried by adding 'appname.modelname' in foreign key field but it actually refrences librehatti.useraccounts.address, which is not what I want.

Any help would be appreciated.

Ilya Kazakevich

unread,
May 28, 2014, 1:48:23 PM5/28/14
to django...@googlegroups.com
Hello,

The easiest way here is to import model module.

from django.db import model
from some_app.some_package import model as foreign_model

class MyClass(model.Model):
foo = model.ForeignKey(foreign_model.SomeForeignModel)

But you also can import model from any installed app as 'app_name.model_class_name'.


Ilya Kazakevich,
JetBrains PyCharm (Best Python/Django IDE)
http://www.jetbrains.com/pycharm/
"Develop with pleasure!"
>--
>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 post to this group, send email to django...@googlegroups.com.
>Visit this group at http://groups.google.com/group/django-users.
>To view this discussion on the web visit
>https://groups.google.com/d/msgid/django-users/dbacf3d8-b4c7-4859-a883-112
>2ab9a9d36%40googlegroups.com
><https://groups.google.com/d/msgid/django-users/dbacf3d8-b4c7-4859-a883-11
>22ab9a9d36%40googlegroups.com?utm_medium=email&utm_source=footer> .
>For more options, visit https://groups.google.com/d/optout.


Satinderpal Singh

unread,
May 28, 2014, 2:02:11 PM5/28/14
to django-users
On Wed, May 28, 2014 at 11:01 PM, Inderpreet Singh <indr...@gmail.com> wrote:
> This is directory structure of my django site
> ---- LibreHatti
> |----- src
> |----- useraccounts
> |----- librehatti
> |------- catalog
> |------- reports
>
> Now what we need is to reference a model from useraccounts in
> librehatti.catalog as a foreign key and I am unable to do that.

You may do it like this models of librehatti.
from useraccount import model.modelname

If you want all useraccounts model in librehatti, then do like,
from useraccounts.models import *

> I tried by adding 'appname.modelname' in foreign key field but it actually
> refrences librehatti.useraccounts.address, which is not what I want.
>
> Any help would be appreciated.



--
Satinderpal Singh
http://satindergoraya91.blogspot.in/
Reply all
Reply to author
Forward
0 new messages