im creating 2 models in django, the first one has django auth user as a foreign key, the second has this first model as a foreign key like this in models.py :
class SGIUsers(models.Model):
charge = models.CharField('Cargo', max_length=80)
user = models.ForeignKey(User, unique=True)
class ResponsibleStateFlow(ModelBase):
user = models.ForeignKey(SGIUsers, verbose_name = 'Responsable', blank = False )
process= models.ForeignKey(Process, verbose_name='Proceso')
But i get this error :
sgiprocess.ResponsibleStateFlow.user: (fields.E300) Field defines a relation with model 'SGIUsers', which is either not installed, or is abstract.
I already imported django auth user of course. Any idea ??
try to add the app name:
field=models.ForeignKey('app_name.ModelName')
Found it, i needed to define an app label with the app name in class meta for SGIUsers like this first:
class Meta:
app_label = 'app_name'
And then call the foreign with 'app_name.Modelname'
Related
i have a problem i don't know how to make multiple many-to-many relations through the same model.
This is my DB relations :
Db architecture
and this is my code, i want to know if what I did is right or not ?
class Projects(models.Model):
Project_name = models.CharField(max_length=50)
Poject_key = models.CharField(max_length=50)
CITools = models.ForeignKey(CITools,null=True,on_delete=models.CASCADE)
UsersO = models.ManyToManyField(User,through='OwnerShips') # for user Ownerships
UserF = models.ManyToManyField(User,through='Favorites') # for user Favorites
This is my OwnerSHips class :
class OwnerShips(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
project = models.ForeignKey(Projects,on_delete=models.CASCADE)
And this is my Favorites Class:
class Favorites(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
project = models.ForeignKey(Projects,on_delete=models.CASCADE)
I don't think you can use foreign keys (ownerships and favorites) in Project before those classes are created.
Actually i found the solution, i used the attribute related_name, so Django to create this two tables automatically
I have a database view that relates 2 companies by foreign keys like so:
DB company_view:
company1_id FK to Company,
company2_id FK to Company,
description text
where
--- some company criteria ---
I try model in Django as unmanaged like so:
class CompanyView(models.Model):
company1 = models.ForeignKey(Company, related_name='company1_id', parent_link=True)
company2 = models.ForeignKey(Company, related_name='company2_id', parent_link=True)
description = models.TextField()
class Meta:
managed = False
db_table = 'company_view'
For the Admin class I have:
#admin.register(models.CompanyView)
class CompanyViewAdmin(AdvancedModelAdmin):
list_display = ('company1', 'company2', 'description')
But the admin page throws exception like:
psycopg2.errors.UndefinedColumn: column company_view.id does not exist
It doesn't make sense to have a primary id key, so is there any way around this?
Thanks
why you dont make a many-to-many relationship?
company = models.ManyToManyField(Company)
look: https://docs.djangoproject.com/en/3.2/topics/db/examples/many_to_many/
this will solve your problem, since it use a object instance.
I am working on the project using django 1.9.
I need to add a field to the user model 'Auth_user' table, the field which i want can be another primary key and act here as foreign key in the 'auth_user'.
I searched a lot but fails. Can any buddy provide me some example how to achieve this like how to to add fields to 'auth_user'
You can substitute the user model entirely as described in doc. Here is an example:
AUTH_USER_MODEL = 'myapp.MyUser'
to your settings.py, and add following to your model:
from django.contrib.auth.models import AbstractUser
class MyUser(AbstractUser):
another_object = models.ForeignKey(OtherModel..
just make a new moel with a user foreign key
class Post (models.Model):
title = models.CharField(max_length=80)
slug= models.SlugField(unique=True)
content = models.TextField()
user_creator = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
there you can use as primary key the id of the post, or the slug (unique), and it can be linked to a user, if you need one to one relationship see this or many to many relationship see this
Maybe you just need to extend de model user
https://docs.djangoproject.com/en/1.11/topics/auth/customizing/#extending-the-existing-user-model
I'm using model like this:
class Product(models.Model):
name = models.CharField(max_length=100)
group = models.ForeignKey(Category.objects.filter(group__category__name='foo'))
issue is getting error django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
You should pass model as ForeignKey argument not a queryset.
If you need to restrcit foreign key choices try to use limit_choices_to:
group = models.ForeignKey(Category, limit_choices_to={'name'='foo'})
I have created two models out of an existing legacy DB , one for articles and one for tags that one can associate with articles:
class Article(models.Model):
article_id = models.AutoField(primary_key=True)
text = models.CharField(max_length=400)
class Meta:
db_table = u'articles'
class Tag(models.Model):
tag_id = models.AutoField(primary_key=True)
tag = models.CharField(max_length=20)
article=models.ForeignKey(Article)
class Meta:
db_table = u'article_tags'
I want to enable adding tags for an article from the admin interface, so my admin.py file looks like this:
from models import Article,Tag
from django.contrib import admin
class TagInline(admin.StackedInline):
model = Tag
class ArticleAdmin(admin.ModelAdmin):
inlines = [TagInline]
admin.site.register(Article,ArticleAdmin)
The interface looks fine, but when I try to save, I get:
Warning at /admin/webserver/article/382/
Field 'tag_id' doesn't have a default value
This can also happen if you have a disused field in your database that doesn't allow NULL.
The problem was that in the DB, tag_id wasn't set as an autoincrement field.
What solved this issue in my case was disabling STRICT_TRANS_TABLES SQL mode which was enabled by default.