What Django command(s) must I use to check if model SomeModelName exists?
Django has an django.apps module with an apps class:
from django.apps import apps
this apps class has a get_models() function, that returns the Model classes (these do not include abstract models, and tables as a result of ManyToManyFields).
We can use .__name__ to obtain the classname. So we can check if SomeModelName exists with:
from operator import attrgetter
'SomeModelName' in map(attrgetter('__name__'), apps.get_models())
Note that this will specify the name of the classes, and that in the different applications you have registered, several models can have the same name (but these are not the same model).
Related
I have myApp/models/profiles.py instead of myApp/models.py (to group related models)
How do you set AUTH_USER_MODEL in this case?
Because auth accepts only "foo.bar" pattern.
app_label, model_name = settings.AUTH_USER_MODEL.split('.')
Django expects the models for any given app to be in app.models. If you want to use this kind of file structure, you'll need to still make sure this is the case. The easiest way to do this is too add from profiles import * in myApp/models/__init__.py and then use AUTH_USER_MODEL as normal.
For example, you you had myApp/models/profiles.py and myApp/models/actions.py your myApp/models/__init__.py should read
from profiles import *
from actions import *
Remember to make sure you don't have any name conflicts too, and you may wish to use set your __all__ value in each of your sub-packages.
You can either pass the class directly or the 'app.model'. django only registers classes defined in app.models module, so you'll need to import the class in models init. Since you'll be importing the model in init anyway, you don't need to specify the full path to the class in the foreignkey.
Currently i have my models, views, forms in default file.
But i want to have directory structure like
articles
---------models
---------views
---------forms
Books
---------models
---------views
---------forms
Icecreams
---------models
---------views
---------forms
so that i keep separately but i don't want different app
The directory structure you describe is the individual apps directory structure. If you want it to look like that, you have to make them separate apps. However, you can do something like:
myapp/
__init__.py
models/
__init__.py
articles.py
books.py
icecream.py
Simply add a models directory and delete models.py. Then, create a separate .py file for each model/group of models you want to separate.
Here's the important part: on each of your models you must add the following to their Meta class:
class MyModel(models.Model):
...
class Meta:
...
app_label = 'myapp'
Where 'myapp', is the main app folder all these models are stored in.
Then, edit models/__init__.py and import all your models there, e.g.:
from .articles import Article
from .books import Book
from .icecream import IceCream
However, the models you list as examples are pretty obvious candidates for distinct apps. The approach above is really only for related models. If your models are truly that distinct, they should be individual apps.
... i don't want different app
But they are different apps, and so should be divided that way.
i'm learning django so i've many questions, and one is how i can reuse a model? i mean the models live in the application folder, but some models are exactly the same between two differents applications.
So should i rewrite the model every time that i write a new app?
Yes, this is wrong when you have the same names of yours apps
You also can use abstract models
class CommonInfo(models.Model):
name = models.CharField(max_length=100)
age = models.PositiveIntegerField()
class Meta:
abstract = True
class Student(CommonInfo):
home_group = models.CharField(max_length=5)
If your models are exactly the same in different applications, you're doing something wrong. Don't forget that an application is basically just a set of models, and you can use one application's models within another application just by importing them.
Can you give an example of two applications with exactly the same models?
How do I reuse a Model.
Best way to reuse model is to Inherit the parent Model class. This is how you must be doing it. Inheriting from models.Model.
from django.db import models
class trial(models.Model):
# override the parent class methods here or define your own
Also make sure that you import your apps models in the appropriate models.py file.
I have a model with a generic relation:
TrackedItem --- genericrelation ---> any model
I would like to be able to generically get, from the initial model, the tracked item.
I should be able to do it on any model without modifying it.
To do that I need to get the content type and the object id. Getting the object id is easy since I have the model instance, but getting the content type is not: ContentType.object.filter requires the model (which is just content_object.__class__.__name__) and the app_label.
I have no idea of how to get in a reliable way the app in which a model is.
For now I do app = content_object.__module__.split(".")[0], but it doesn't work with django contrib apps.
The app_label is available as an attribute on the _meta attribute of any model.
from django.contrib.auth.models import User
print User._meta.app_label
# The object name is also available
print User._meta.object_name
You don't need to get the app or model just to get the contenttype - there's a handy method to do just that:
from django.contrib.contenttypes.models import ContentType
ContentType.objects.get_for_model(myobject)
Despite the name, it works for both model classes and instances.
You can get both app_label and model from your object using the built-in ContentType class:
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import User
user_obj = User.objects.create()
obj_content_type = ContentType.objects.get_for_model(user_obj)
print(obj_content_type.app_label)
# u'auth'
print(obj_content_type.model)
# u'user'
This is better approach respect of using the _meta properties that are defined for private purposes.
In one of my views I am importing models from two apps:
from mysite.business.models import Location
from mysite.directory.models import Location
As you can see, both Models have the same name. If I want to create an instance of one of these models, how do I define which one I require?
Give them different names.
from mysite.business.models import Location as BusinessLocation
from mysite.directory.models import Location as DirectoryLocation