django-oscar RuntimeError: conflicting models - django

Version Info: Python 2.7, Django 1.9, Oscar Commerce - VERSION = (1.3)
I am trying to customize Products and few other models in the catalogue app following the documentation.
I have forked catalogue app (to myproject/forked_apps/catalogue) as per documentation documentation and did this in models.py:
from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct
class Product(AbstractProduct):
is_active = models.BooleanField(default=False)
from oscar.apps.catalogue.models import *
I have already included the modified catalogue app, in the INSTALLED_APPS in settings.py as an argument for get_core_apps function, as stated in docs (so my local app is replacing the original app from Oscar).
INSTALLED_APPS = [
...
] + get_core_apps(['forked_apps.catalogue'])
Migrations are also copied from oscar.apps.catalogue to my local app.
When I'm trying to make migrations I'm getting this error all the time:
RuntimeError: Conflicting 'product_product_options' models in application 'catalogue': <class 'oscar.apps.catalogue.models.Product_product_options'> and <class 'forked_apps.catalogue.models.Product_product_options'>.
I tried to remove all migrations from my local catalogue app (the I copied before from Oscar app), then it works, but all new migrations are created in Oscar source code folder, but I need them to be in my project...
How do I get over this error ?

Make sure you're using the following wherever you use the Product model:
from oscar.core.loading import get_model
Product = get_model('catalogue', 'Product')
if you, in some place of your code write an import just like this:
from oscar.apps.catalogue.models import Product
you will ran into this issue.

Related

Django unable to load model into views

I am trying to import my models into views.py but I am unable to do so. However I am able to register them on the admin site but when I use the same code I used in admin.py to import the models into views.py, I get an error. I am using djongo so I am not sure if that changes anything about how to import them and I cannot seem to find the documentation for it.
models.py
from djongo import models
class Round(models.Model):
round_num = models.IntegerField(default=0)
admin.py
from django.contrib import admin
from .models import Round
admin.site.register(Round)
views.py
from .models import Round
When I try and run my views.py file I get the following error: ModuleNotFoundError: No module named 'main.models'; 'main' is not a package
Also my views, admin, and models file are all in the same directory. I have made the migrations and I can see my Round model in MongoDB. The only thing I cannot do is import it to the view
You need to have an __init__.py file in your directory. It should be inside of your main folder and at the same level as your views.py and models.py
As a workaround, since the models properly migrate to MongoDB. Using pymongo I have just connected to Mongo and have rendered data into my views this way. It works fine so if anybody else has an issue loading in their models, you can always just connect directly to the DB.

RuntimeError: Conflicting 'product_product_options' models in application 'catalogue'

Version Info:
Python 3.4, Django 1.8, Oscar Commerce - VERSION = (1, 2, 1, 'final')
I am trying to customize Products in the catalogue app following the documentation.
Having forked the catalogue app, I have defined models.py as follows:
from django.db import models
from oscar.apps.catalogue.abstract_models import AbstractProduct
class Product(AbstractProduct):
is_active = models.BooleanField(default=False)
from oscar.apps.catalogue.models import *
I have already included the modified catalogue, in the INSTALLED_APPS in settings.py as a list, as suggested for a similar problem here.
INSTALLED_APPS = INSTALLED_APPS + get_core_apps(
['app.gravytrain.catalogue',])
Have copied the migration folder from oscar/apps/catalogue to my custom app.
However running migration causes the following error:
RuntimeError: Conflicting 'product_product_options' models in
application 'catalogue': <class
'gravytrain.catalogue.models.Product_product_options'> and <class
app.gravytrain.catalogue.models.Product_product_options'>.
How do I get over this error ?
If you want to import some models, you need use get_model function.
For example:
from oscar.core.loading import get_model
Product = get_model('catalogue', 'Product')
I had the same error. I have also included "from oscar.apps.catalogue.models import *" in the top of the model. Once I was removed it, that issue fixed.

Add all models to admin site django 1.9

I need to add my model to my admin site on django 1.9,
I was using this code earlier:
for model in get_model(get_app('campaign')):
admin.site.register(model)
However, it isn't supported by django 1.9 verison. Is there a new way to do this?
You can use the get_models() method of the app config.
from django.contrib import admin
from django.apps import apps
myapp = apps.get_app_config('myapp')
for model in myapp.get_models():
admin.site.register(model)
In modern versions of Django (I'm not sure whether it will work for Django < 1.10), you can simplify this to:
myapp = apps.get_app_config('myapp')
admin.site.register(myapp.get_models())

Django 1.5.1 - Admin.py missing while running startapp

I've been following the DjangoProject tutorial. When I run python manage.py startapp newapp while in the same directory as manage.py. In the newapp directory I see init.py, models.py, tests.py, and views.py but not admin.py file. Where is admin.py?
I am running Django 1.5.1 in Windows 8
You have to create an admin.py file.
you don't necessarily need an admin.py file,
just import the admin module in your models.py file,
from django.contrib import admin
and for each model do the following:
admin.site.register(model1)
admin.site.register(model2)
However, this is not best practice, but since it's just a tutorial, it will work.
You also need to uncoment the relevant lines in the urls.py file
I think I had the same frustrations following the DjangoProject tutorial - however, when I cross-referenced it with with the DjangoBook tutorial (for the same version, I believe, 1.5.1), I found that an admin.py file was not necessarily created after a python manage.py startapp xyz command -- moreover, I also uncommented all of the admin options in urls.py, views.py, and settings.py - so a bit of a mix of what Neal and Ibrahim said
You have to create your own admin.py file in the app if you want it. Indeed, this file is optionnal and isn't created by startapp.
If you want a default template to begin your admin.py, it should be:
from django.contrib import admin
from models import Model1, Model2
class Model2Admin(admin.ModelAdmin):
list_display = ('title', 'content', 'date')
# Just an example, chekc docs and tutorials for more info.
admin.site.register(Model1)
admin.site.register(Model2, Model2Admin)
The reason there is no default admin.py is because you don't have any models yet when you create your new application; so there is nothing to add to the admin section.
Further, you may not want to admin all the models in your application; or you may be creating an application that does not need any admin hookups; or you may not be using the admin application at all in your project.
Since django cannot decide this for you, there is no default admin.py generated.
To create one, if you are following the tutorial - simply keep reading and in part two you'll create the admin.py file when you learn about the admin contrib app and how to integrate it with your custom models.

Add a field to Mezzanine blogpost

I am using Mezzanine for a project. I need to add a extra field to Mezzanine blogpost.
I notice using EXTRA_MODEL_FIELDS can do it, but it looks complex.
I also try copy the blog folder from the site-package to my project path, and then modify the models.py. but I doesn't work.
I am new to Django, can some one help?
Thanks
By do some research, now I got the answer:
1. copy the blog app from sites-package to my project
2. change my setting.py
INSTALLED_APPS = (
"blog", #it was "mezzanine.blog",
.....
3. modify the blog/models.py
add following line to class BlogPost
shop_url= models.CharField(max_length=250,null=True, blank=True)
4. migirate the table (installed South)
./manage.py schemamigration blog --auto
./manage.py migrate blog
You can create a django app (CustomBlog), add it to your installed apps
and remove or comment the Mezzanine blog:
INSTALLED_APPS = (
"CustomBlog", #it was "mezzanine.blog",
...
)
In the models.py and admin.py, of your CustomBlog, inherit from the class BlogPost of Mezzanine:
models.py
from django.db import models
from mezzanine.blog.models import BlogPost
from mezzanine.blog.models import BlogCategory
class CustomBlog(BlogPost):
# Add New Field
# example
new_field = models.CharField(max_length=255)
class CustomBlogCategory(BlogCategory):
pass
admin.py
from django.contrib import admin
from .models import CustomBlog,CustomBlogCategory
admin.site.register(CustomBlog)
admin.site.register(CustomBlogCategory)
Then in the terminal create and run migrations
python manage.py makemigrations
python manage.py migrate