Import model error from one app to another app - python

I have 2 app inside a django project. I want to import a model from one app to another. But it gives me
NameError: name 'JobGenre' is not defined
when I try to syncdb
In customer.models
from job.models import JobGenre
class Worker(Costumer):
keyword=models.ForeignKey(JobGenre, null=True)
and in job.models
class JobGenre(models.Model):
genre=models.CharField(max_length=40)
if i use
keyword=models.ForeignKey('job.models.JobGenre', null=True)
it gives
Error: One or more models did not validate:
costumer.worker: 'keyword' has a relation with model job.models.JobGenre, which has either not been installed or is abstract.
What should I do in this situation?

keyword=models.ForeignKey('job.models.JobGenre', null=True)
Looks incorrect to me.
Try instead:
keyword.models.ForeignKey('job.JobGenre', null=True)

You don't need to give the full package path to ForeignKey method. Just giving appname.modelclass will work.
keyword=models.ForeignKey('job.JobGenre', null=True)
should work. please refer here.

Related

Why leads deletion of UUIDField to Django SystemCheckError

I've been building a Django website and included a UUID field "customer_id" in my initial "Customer" model. Finally, I decided to drop it. But when I try to delete it from my models.py, Django throws
SystemCheckError: System check identified some issues:
ERRORS:
<class 'accounts.admin.CustomerAdmin'>: (admin.E035) The value of 'readonly_fields[1]' is not a callable, an attribute of 'CustomerAdmin', or an attribute of 'accounts.Customer'.
Here is the code of models.py
from django.db import models
import uuid
# Create a base model to make sure we keep track of creation and edits
class ModelBaseClass(models.Model):
date_created = models.DateTimeField(auto_now_add=True, null=True)
date_modified = models.DateTimeField(auto_now=True, null=True)
class Meta:
abstract = True
# Create your models here.
class Customer(ModelBaseClass):
customer_id = models.UUIDField(default=uuid.uuid4, #this is the field i try to drop
editable=False,
unique=True)
name = models.CharField(max_length=200, null=True)
email = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
What I tried so far:
I suspect that this could be related to existing data or some other dependencies. So...
I deleted the sqlite database, deleted all migration files and ran
"python manage.py makemigrations" and "python manage.py migrate".
I ran python manage.py flush.
I also tried to change the editable=False to editable=True and migrate before dropping,
but it didn't change anything.
It's perhaps also worth mentioning that my "Customer" model a relation to another model.
Could someone explain me why Django is preventing me from deleting this field and how to resolve this?
Thanks! :)
Could someone explain me what's going on and how to resolve this?
As the error says, you have a model admin named CustomerAdmin. Indeed:
<class 'accounts.admin.CustomerAdmin'>: (admin.E035) The value of 'readonly_fields[1]' is not a callable, an attribute of 'CustomerAdmin', or an attribute of 'accounts.Customer'.
For the readonly_fields, it lists the customer_id, but since that field is no longer available, it raises the error.

Why is Django giving me a ValueError when I reference a class within the same model?

I'm building a simple recipe app, and so far I have two models: Ingredient and Recipe.
Each recipe should have multiple ingredients, so I laid out my model like this:
class Ingredient(models.Model):
name = models.CharField(max_length=50)
class Recipe(models.Model):
title = models.CharField(max_length=100)
ingredients = models.ForeignKey(Ingredient, on_delete=CASCADE)
instructions = JSONField()
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=SET_DEFAULT, default='Chef Anon')
When I makemigrations, I get nothing, but when I migrate, I get this ValueError:
ValueError: Cannot alter field cookbook.Recipe.ingredients into cookbook.Recipe.ingredients - they do not properly define db_type (are you using a badly-written custom field?)
Following the example here (Django: Add foreign key in same model but different class), I've tried setting ingredients=models.ForeignKey(Ingredient, on_delete=CASCADE) as well as using lazy syntax ingredients=models.ForeignKey("Ingredient", on_delete=CASCADE), but each time, makemigrations shows no changes, and migrate gives me the same ValueError.
Edit
My imports:
from django.db.models.deletion import CASCADE, SET_DEFAULT, SET_NULL
from django.db.models.fields.json import JSONField
from django.utils import timezone
from django.contrib.auth.models import User```
Try replacing on_delete=CASCADE with on_delete=models.CASCADE
If you have not imported CASCADE separately from models.
All though, in that case you should get a warning that "CASCADE is not defined".
I believe I found the problem: My models.py file was in the root directory, not in the app directory.

ModuleNotFoundError: No module named 'project.app' while trying to add foreignkey constraint?

Here is my models.py file:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class CategoryList(models.Model):
Category = models.CharField(max_length=200)
Cat_Img = models.ImageField(upload_to='cat_media')
Active = models.BooleanField(default=True)
class ImgDetails(models.Model):
Img = models.ImageField(upload_to='media')
Category = models.ForeignKey(CategoryList, default=1, on_delete=models.SET_DEFAULT, null=False)
keywords = models.CharField(max_length=255)
UserDetail = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
Valid = models.BooleanField(default=False)
UploadDate = models.DateTimeField(auto_now_add=True)
I'm trying to add a foreign-key constraint on ImgDetails with CategoryList. It is throwing error
from cam_project.cam_app.models import CategoryList
ModuleNotFoundError: No module named 'cam_project.cam_app'
I tried importing from cam_project.cam_app.models import CategoryList but still no progress. Any suggestions would be appreciated.
If your models.py is in the cam_app you can just use:
from .models import CategoryList
or if you are importing another app's models.py, you should specify the app name as well:
from .app.models import Model
Be sure the directory that contains cam_project must be in the PYTHONPATH.
You can check it inspecting sys.path.
To add it you can:
export PYTHONPATH=<YOUR_DIR>:$PYTHONPATH
I don't think the error is in models.py. Your foreign-key constraint is working fine. It seems you are importing CategoryList in some other file(my best guess views.py) In django absolute import are relative to your manage.py. So I think you import should be
from cam_app.models import CategoryList

How do I fix this error, what does it mean Category has no attribute 'get_impath'?

I wrote tests for my model, but there is an error. I cannot fix that.
The error message AttributeError: 'Category' object has no attribute 'get_impath'
from django.test import TestCase
from .models import *
class CategoryTest(TestCase):
def setUp(self):
Category.objects.create(name='Python', impath='img.png')
def test_category_models(self):
category_test = Category.objects.get(name='Python')
self.assertEqual(
category_test.get_impath(), "img.png")
Here is my model
from django.db import models
class Category(models.Model):
name = models.CharField(max_length=15)
impath = models.CharField(max_length=64)
Category.objects.create(name='Python', impath='img.png')
I assume this means you have something like
class Category(models.Model):
name = models.CharField(max_length=255)
impath = models.ImageField()
Here impath is an attribute. Django does not automatically create a method named get_impath(). This means you need to do category_test.impath instead of category_test.get_impath(). Fields in your model are attributes and there are no get methods for them unless you write them yourself.
Note that you are writing a test that verifies that Django works the way the documentation says it will. This is good for learning about how Django works and how to write tests in the Django environment. However, beyond that, such tests are not worth very much because you should assume that the authors of Django have already tested it.

Django models.py Circular Foreign Key

I have a django app which basically is just a photo album. Right now I have two models: Image and Album. Among other things, each Album has a foreign key to an Image to be its thumbnail and each Image has a foreign key to the Album it belongs in. However, when I try to use manage.py syncdb or manage.py sqlall I get errors saying the class not defined first in models.py isn't defined when it is used in the first class defined.
models.py (abridged):
from django.db import models
import os
class Album(models.Model):
thumb = models.ForeignKey(Image, null=True, blank=True)
class Image(models.Model):
image = models.ImageField(upload_to='t_pics/images')
thumb = models.ImageField(upload_to='t_pics/images/thumbs')
album = models.ForeignKey(Album)
Error I get when I do manage.py sqlall appname:
[...]
File "/path/to/file/appname/models.py", line 4, in ?
class Album(models.Model):
File "/path/to/file/appname/models.py", line 5, in Album
thumb = models.ForeignKey(Image, null=True, blank=True)
NameError: name 'Image' is not defined
I get the same error when I switch the order of the classes in models.py except it says 'Album' undefined instead of 'Image' undefined I also tried commenting the dependancy in the first class then uncommenting after everything else was successfully imported but that didn't help. How should I go about making this work? I'm reluctant to make an entire third class Thumb because it will have a lot of the same code as Image I'm also pretty sure I could manually add the foreign key to the database but I want this to be clean and not hackish.
You don't actually have a circular reference; the issue is that, at the time you define Album, you haven't defined Image yet. You can fix that by using a string instead:
class Album(models.model):
thumb = models.ForeignKey('Image', null=True, blank=True)
However, in this case, you might want to use a OneToOneField instead of a foreign key. (Note that you'll still have to use the trick with the string, though).
Use quotes to force a lazy reference:
models.ForeignKey('Image', null=True, blank=True)
Also, ForeignKey.related_name is your friend (avoids back-reference name clashes).
This is old but anyway, i'd like to say that I don't see a reason for attribute album in model Image. In my opinion, it is not really needed.

Categories