I am using oscarcommerce for my django project. I want to extend the "StockRecord" model to include some more fields. So I forked the partner app as follows. (boscar is my app name)
python manage.py oscar_fork_app partner boscar/
It successfully forked and new files were added to boscar/partner folder. I added 'boscar.partner' in my installed apps.
Now I added new fields in StockRecord models as follows
boscar/partner/models.py
from django.db import models
from oscar.apps.partner.abstract_models import AbstractStockRecord
class StockRecord(AbstractStockRecord):
effective_price = models.FloatField(default=0, null=True)
is_instock_item = models.BooleanField(default=False, null=True)
instock_quantity = models.IntegerField()
from oscar.apps.partner.models import * # noqa
Now when I try to make migrations it shows the following error.
RuntimeError: Conflicting 'stockrecord' models in application 'partner': <class 'oscar.apps.partner.models.StockRecord'> and <class 'boscar.partner.models.StockRecord'>.
I already successfully forked the catalogue and order models and that are working fine. Only this "StockRecord" models showing this error.
That error can occur as a result of a circular import issue associated with Oscar's support for overriding models and classes.
You need to check for places where you're importing directly from oscar.apps.partner.models. These should be replaced by importing from boscar.partner.models or using oscar.core.loading.get_model.
Related
When I'm trying to migrate a new app onto the server i get this error
AttributeError: module 'django.db.models' has no attribute 'Models'- in terminal
I'm using PyCharm. I am very fresh to Django and web development so any tips will help. Thanks!
from django.db import models
# Create your models here.
class product(models.Model):
item = models.Textfiels()
description = models.Textfields()
price = models.Textfields()
There's no such class django.db.models.TextFields but this works for me on any recent version :
from django.db import models
class product(models.Model):
item = models.TextFiel()
description = models.TextField()
price = models.TextField()
You made 2 typos : the correct name is TextField and you typed Textfields (Python is case sensitive)
I suspect you didn't setup correctly your project under PyCharm. When correctly setup, it shows warnings on misspelled names (names underlined with red dots with default setting).
There's another variation to this question and that is in the form of:
AttributeError: module 'django.contrib.auth' has no attribute 'models'
As far as I can tell this is typically caused by conflicting imports or improperly imported files. Another cause could be changes to Django's updates but I'm not sure about that as I didn't find any documentation that changed that aspect of the Django library.
Short term solution to this is as follows:
from django.contrib.auth import models
class MyClass(models.User): """ """
This will allow you to at least test your runserver command and website on a browser of your choosing.
I'm still trying to figure out any other solutions to this problem that can be a fix for individually importing the 'auth' module itself.
At the time of this writing I'm using Django 2.2.6 whereas Django 2.2.7 is out and 2.2.8 is on the way to be released.
I'm not sure if this is the solution , but when I had this problem it was because in my admin.py file I had
from django.contrib import admin
from meetings.models import Meeting, Room
admin.site.register(Meeting, Room)
But changing it to solved the issue
from django.contrib import admin
# Register your models here.
from meetings.models import Meeting, Room
admin.site.register(Meeting)
admin.site.register(Room)
after customizing django oscar app (address) and adding a new field named 'area' when I run migrate it gave me Unknown field(s) (area) specified for UserAddress
I used the command
./manage.py oscar_fork_app address myappsfolder/
after creating the folder and __init__.py file in it
then I started to customize the app like this:
#myappsfolder/address/models.py
from django.db import models
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from oscar.apps.address.abstract_models import AbstractAddress
class Address(AbstractAddress):
area = models.CharField(_("Area"), max_length=120,
choices=settings.AREA_CHOICES, blank=True)
from oscar.apps.address.models import *
#myappsfolder/address/forms.py
from oscar.apps.address import forms as base_forms
class UserAddressForm(base_forms.UserAddressForm):
class Meta(base_forms.UserAddressForm.Meta):
fields = ['area']
I didn't touch the admin.py , config.py and __init__.py that have been created by the command ./manage.py oscar_fork_app address myappsfolder/
also the __init__.py that I created in myappsfolder is empty, should I add something to these files ?
What should I do to customize this app ? or any other app ?
If I edited the models in oscar/apps/address/abstract_models.py it only apply in local host and the error in forms disappear , which means that django still reading the models from oscar/apps not from myappsfolder.
You cannot override AbstractAddress in that way because the changes will not propagate to other models such as UserAddress that directly subclass AbstractAddress. Instead you need to override the specific address models that you want to change. So if you want to change UserAddress then override that particular model:
from oscar.apps.address.abstract_models import AbstractUserAddress
class UserAddress(AbstractUserAddress):
area = models.CharField(_("Area"), max_length=120, choices=settings.AREA_CHOICES, blank=True)
... after which your changes to UserAddressForm should work.
If you need to override ShippingAddress as well, then you will also have to fork the shipping app and do the same thing there, because that is where the model lives.
I'm trying to update an old django app from 1.1.x to 1.8 LTS, which has involved updating paths, as I apps move apps around.
However, I'm unable to generate migrations for one app, and I can't see how to reference a namespaced app model correctly (assuming that's the problem)
If I've moved files from PROJECT/news/models to PROJECT/site_name/news/models, how should I be referencing these models in in foreign keys or ManyToManyFields?
My app
I have a projects app I want to make migrations for. Projects in some_org/projects, and listed in installed apps like so:
INSTALLED_APPS = (
'some_org.maps',
'some_org.library',
'some_org.extras',
'some_org.news',
'some_org.projects',
'some_org.members',
'some_org.comments',
)
All the apps with the namespace are within the some_org directory.
Here's an abridged view of the models file in the projects app:
# some_org/projects/models.py
from some_org.library import Paper
class Project(models.Model):
name = models.CharField(max_length=30)
def get_children(self):
return ProjectPage.objects.filter(level=1, publish=True, project=self)
def has_library(self):
return Paper.objects.filter(projects=self).count() > 0
Calling ./manage.py makemigrations library, gives me this error:
ValueError: Lookup failed for model referenced by field library.Paper.projects: projects.Project
When I look in the Paper model, it looks like this:
class Paper(models.Model):
# snip
# NewsLinkSubject, Projects et al used to in an app on
# the project root, like `./app_name/models.py`, but is now
# in `some_org/app_name/models.py`
subjects = models.ManyToManyField("news.NewsLinkSubject", blank=True)
projects = models.ManyToManyField("projects.Project", blank=True,)
country = models.ForeignKey("maps.Country", null=True, blank=True)
I initially wonder if the label for the app is wrong, and try the projects ManytoMany field to:
projects = models.ManyToManyField("some_org.projects.Project", blank=True,)
This gives a different error:
ERRORS:
library.Paper.subjects: (fields.E300) Field defines a relation with model some_org.projects.Project', which is either not installed, or is abstract.
As far as I can tell the app is installed, and the models aren't abstract.
I'm pretty stumped - what am I doing wrong, and how can I fix this so I can make migrations for these apps?
I'm using Django 1.8.17, and Python 2.7.13.
You should define an app_label in the Meta class for your models. Whatever you put in there becomes the first part of the name you use in the ManyToManyField definition.
I created following custom user model by using "AbstractUser" model.
from django.conf.global_settings import AUTH_USER_MODEL
MyCustomUser(AbstractUser):
regions = models.ManyToManyField(to='SomeModel')
class Meta:
app_label = 'myapp'
MyObject(models.Model):
owner = models.ForeignKey(to=AUTH_USER_MODEL)
And I set AUTH_USER_MODEL to settings/base.py (I separated setting file each environment. e.x: settings/base.py, settings/development.py).
AUTH_USER_MODEL = 'myapp.MyCustomUser'
When I executed python manage.py syncdb, my console window has flared up by Django!
myapp.MyObject: 'owner' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.
So, I had two questions.
Is the problem is that I separated settings.py each environment.
Does from django.global_settings import AUTH_USER_MODEL import myproject.settings?
And I resolved from myproject.settings.base import AUTH_USER_MODEL in this time.
global_settings is, as the name implies, the global default settings supplied by Django. So of course the default is 'auth.User'.
Since you override it in your own settings, you should import that instead. But as the documentation says, the way to import the current settings is always from django.conf import settings, rather than explicitly importing a settings file in your project.
Also note though that rather than using the settings at all here - which could lead to some dependency issues on startup - you should be using get_user_model() from django.contrib.auth, as explained in the documentation.
I use mongoengine with django. I have two applications with models.
app1/models.py:
from mongoengine import fields
from mongoengine.document import Document
class Model1(Document):
name = fields.StringField()
lists = fields.ListField(fields.ReferenceField("Model2", dbref=False))
app2/models.py:
from mongoengine import fields
from mongoengine.document import Document
class Model2(Document):
name = fields.StringField()
All applications were added to INSTALLED_APPS. When I use the django dev-server, everything is fine. But using this code with uwsgi-server there is an error:
Model2 has not been registered in the document registry.
Importing the document class automatically registers it, has it
been imported?
What I should do?
You should import app2.models somewhere. Put a comment by the import saying why it's there, so nobody removes the useless-looking import in the future.
When the django dev server starts up it imports the models from all installed apps and validates them. You'll see
Validating models...
0 errors found
This does not happen in a production environment. It is just a nicety of the dev server.