Here is what appears when i try to use python manage.py makemigrations
traceback error
Here is models.py code
----------------------code-------------------------------------
from django.db import models
from django.contrib.auth.models import User
class Category(models.Model):
name = models.CharField(max_length=255, db_index=True)
slug = models.SlugField(max_length=255, unique=True)
class Meta:
verbose_name_plural = 'categories'
# def get_absolute_url(self):
# return reverse("store:category_list", args=[self.slug])
def __str__(self):
return self.name
class Product(models.Model):
category = models.ForeignKey(Category, related_name='product', on_delete=models.CASCADE)
created_by = models.ForeignKey(User, on_delete=models.CASCADE, related_name='product_creator')
title = models.CharField(max_length=255)
author = models.CharField(max_length=255,default='admin')
description = models.TextField(blank=True)
image = models.ImageField(upload_to='images/')
slug = models.SlugField(max_length=255)
price = models.DecimalField(max_digits=4, decimal_places=2)
in_stock = models.BooleanField(default=True)
is_active = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
class Meta:
verbose_name_plural = 'Products'
ordering = ('-created',)
def __str__(self):
return self.title
------------------code end-------------------------------
im new and i dont know whats the problem
Related
Hello I was wondering could anyone help me with an issue I've ran into while working on a Django project. I've tried a few different things but cant seem to get it working a screenshot of the error is attached.
[![enter image description here][1]][1]
error
[1]: https://i.stack.imgur.com/LKTPu.png
import uuid
from django.db import models
from django.urls import reverse
class Category(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=250, unique=True)
description = models.TextField(blank=True)
image = models.ImageField(upload_to='category', blank=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def get_absolute_url(self):
return reverse('phoneshop:products_by_category', args=[self.id])
def __str__(self):
return self.name
class Product(models.Model):
id = models.UUIDField(
primary_key=True,
default=uuid.uuid4,
editable=False)
name = models.CharField(max_length=250, unique=True)
description = models.TextField(blank=True)
category = models.ForeignKey(Category, on_delete=models.CASCADE)
price = models.DecimalField(max_digits=10, decimal_places=2)
image = models.ImageField(upload_to='product', blank=True)
stock = models.IntegerField()
available = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True, blank=True, null=True)
updated = models.DateTimeField(auto_now_add=True, blank=True, null=True)
class Meta:
ordering = ('name',)
verbose_name = 'product'
verbose_name_plural = 'products'
def get_absolute_url(self):
return reverse('phoneshop:prod_detail', args=[self.Category.id, self.id])
def __str__(self):
return self.name
The App is A Blog Post App That is Working fine Locally But After I Deployed It To Heroku, The App Cannot run. I did more Than a week trying to fix it through the views and models with no success.
This Is The Error When Trying To Open The App
views.py
from django.shortcuts import render,get_object_or_404, get_list_or_404
from posts.models import Post, Category
# Create your views here.
import datetime
def index(request):
now = datetime.datetime.now()
big_slider_list = Post.objects.filter(category__name='Big_Slider')
news_box_list = Post.objects.filter(category__name='People')
cote_divoire_list = Post.objects.filter(category__name="CĂ´te d'Ivoire").exclude(position=1)
block1_big = Post.objects.filter(position=1)
culture_list = Post.objects.filter(category__name="Culture").exclude(position=2)
block2_big = Post.objects.filter(position=2)
sport_list = Post.objects.filter(category__name="Sports").exclude(position=3)
block3_big = Post.objects.filter(position=3)
infrast_et_devel_list = Post.objects.filter(category__name="Infrastructures Et DĂ©velopements")
context = {
'now': now,
'big_slider_list': big_slider_list[:10],
'news_box_list': news_box_list[:4],
'cote_divoire_list': cote_divoire_list[:4],
'block1_big': block1_big[0],
'culture_list': culture_list[:3],
'block2_big': block2_big[0],
'sport_list': sport_list[:3],
'block3_big': block3_big[0],
'infrast_et_devel_list': infrast_et_devel_list[:3],
}
return render(request, 'home/index.html', context)
models.py :
from django.db import models
from django.contrib.auth import get_user_model
from django.urls import reverse
# Create your models here.
User = get_user_model()
STATUS = (
(0, "Draft"),
(1, "Publish")
)
POSITIONS = [
(0, ' '),
(1, 'BIG_BLOCK_1'),
(2, 'BIG_BLOCK_2'),
(3, 'BIG_BLOCK_3'),
(4, 'BIG_BLOCK_4'),
]
class Author(models.Model):
name = models.CharField(max_length=50)
user = models.OneToOneField(User, on_delete=models.CASCADE)
profile_picture = models.ImageField(null=True, blank=True)
email = models.EmailField(unique=True)
active = models.BooleanField(default=False)
created_on = models.DateTimeField(auto_now_add=True)
last_logged_in = models.DateTimeField(auto_now=True)
def __str__(self):
return self.user.username
class Category(models.Model):
country = models.CharField(max_length=200, blank=True)
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE,)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('posts:post_by_category', args=[self.slug])
class Tag(models.Model):
name = models.CharField(max_length=100, unique=True)
slug = models.SlugField(max_length=100, unique=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE,)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('posts:post_by_tag', args=[self.slug])
class Post(models.Model):
status = models.IntegerField(choices=STATUS, default=0)
position = models.IntegerField(choices=POSITIONS, default=0)
title = models.CharField(max_length=300)
short_title = models.CharField(max_length=200)
display_image = models.ImageField(null=True, blank=True)
post_image = models.ImageField(null=True, blank=True)
content = models.TextField(max_length=8000)
slug = models.SlugField(max_length=300, unique=True)
updated_on = models.DateTimeField(auto_now=True, auto_now_add=False)
created_on = models.DateTimeField(auto_now=False, auto_now_add=True)
author = models.ForeignKey(Author, on_delete=models.CASCADE,)
category = models.ForeignKey(Category, on_delete=models.CASCADE,)
tags = models.ManyToManyField(Tag,)
comment_count = models.IntegerField(default=0)
class Meta:
ordering = ['-created_on']
verbose_name = "All Post"
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('posts:post_detail', kwargs={'slug': self.slug})
class Comment(models.Model):
post = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='comments')
name = models.CharField(max_length=80)
email = models.EmailField()
body = models.TextField()
created_on = models.DateTimeField(auto_now_add=True)
active = models.BooleanField(default=False)
class Meta:
ordering = ['created_on']
def __str__(self):
return 'Comment {} by {}'.format(self.body, self.name)
It's something to do with the migrations. Your local DB has all the migrations applied, while your Database on Heroku doesn't have the post field. Maybe you added/edited the field later so your two DBs are out of sync. Since this seems to be a new project without data it would be easier for you to heroku reset your heroku database and push the local db to heroku. Here are the instructions: https://devcenter.heroku.com/articles/heroku-postgres-import-export
from django.db import models
from django.utils import timezone
from django.contrib.auth.models import User
# Create your models here.
class Product(models.Model):
CONDITION_TYPE = (
("New", "New"),
("Used", "Used"),
)
name = models.CharField(max_length=50)
category = models.ForeignKey(
'Category', on_delete=models.SET_NULL, null=True)
brand = models.ForeignKey(
'Brand', on_delete=models.SET_NULL, blank=True, null=True)
description = models.TextField()
owner = models.ForeignKey(User, on_delete=models.CASCADE)
condition = models.CharField(max_length=50, choices=CONDITION_TYPE)
price = models.DecimalField(max_digits=10, decimal_places=2)
created = models.DateTimeField(default=timezone.now)
def __str__(self):
return self.name
class Category(models.Model):
category_name = models.CharField(max_length=50)
image = models.ImageField(upload_to='product/', blank=True, null=True)
def __str__(self):
return self.category_name
class Meta:
verbose_name = 'category'
verbose_name_plural = 'categories'
class Brand(models.Model):
category_name = models.CharField(max_length=50)
def __str__(self):
return self.category_name
class Meta:
verbose_name = 'brand'
verbose_name_plural = 'brands'
class ProductImages(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
product_image = models.ImageField(
upload_to='categories/', null=True, blank=True)
def __str__(self):
return self.Product
class Meta:
verbose_name = 'product images'
verbose_name_plural = 'product images'
When I am adding the product images in the admin panel section, It is showing the attribute error:
AttributeError at /admin/product/productimages/add/
'ProductImages' object has no attribute 'Product'
Request Method: POST
Request URL: http://127.0.0.1:8000/admin/product/productimages/add/
Django Version: 3.0.5
Exception Type: AttributeError
Exception Value:
'ProductImages' object has no attribute 'Product'
Exception Location: /home/durga/Desktop/django_projects/olx_clone/src/product/models.py in __str__, line 59
Python Executable: /home/durga/Desktop/django_projects/olx_clone/bin/python
Python Version: 3.7.5
Python Path:
['/home/durga/Desktop/django_projects/olx_clone/src',
'/usr/lib/python37.zip',
'/usr/lib/python3.7',
'/usr/lib/python3.7/lib-dynload',
'/home/durga/Desktop/django_projects/olx_clone/lib/python3.7/site-packages']
Server time: Wed, 29 Apr 2020 12:03:20 +0000
In Models:
class ShopCategory(models.Model):
category = models.CharField(max_length=100)
class Meta:
db_table = "tbl_ShopCategory"
class Shop(models.Model):
id_shop = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
slug = models.SlugField(max_length=250, blank=True, null=True)
cover_img = models.CharField(max_length=250, blank=True)
category = models.ForeignKey(ShopCategory, on_delete=models.CASCADE)
avgscore = models.FloatField(default=0)
I want to show the Category name?
...........................................................
Add an str method to your ShopCategory model:
class ShopCategory(models.Model):
category = models.CharField(max_length=100)
def __str__(self):
return self.category
class Meta:
db_table = "tbl_ShopCategory"
I m new to Django and I am learning it through a project but m stuck with this error which says that NoReverseMatch at /product/create/
here is my models.py file
from django.db import models
from django.conf import settings
from django.core.urlresolvers import reverse
# Create your models here.
class Category(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='category_created')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True, unique=True)
class Meta:
ordering = ('name',)
verbose_name = 'category'
verbose_name_plural = 'categories'
def __str__(self):
return self.name
class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='product_created', null= True,blank=True)
category = models.ForeignKey(Category, related_name='products')
name = models.CharField(max_length=200, db_index=True)
slug = models.SlugField(max_length=200, db_index=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
negiotiable = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='product_liked',
blank=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('products_detail',
args=[self.slug])
This is my views.py but I m not sure if there is something wrong with my views
from django.views.generic import *
from django.core.urlresolvers import reverse_lazy
from .models import Category, Product
class CategoryList(ListView):
model = Category
class CategoryDetail(DetailView):
model = Category
class ProductList(ListView):
model = Product
class ProductDetail(DetailView):
model = Product
class ProductCreate(CreateView):
model = Product
fields = ["category", 'name', 'image', 'description', 'price', 'stock','available', 'negiotiable']
class ProductUpdate(UpdateView):
model = Product
fields = ['name', 'image', 'description', 'price', 'stock','available', 'negiotiable']
class ProductDelete(DeleteView):
model = Product
success_url = reverse_lazy('product_list')
I can't tell if this is the problem without the traceback or urls.py but I'm guessing you need to auto-generate the slug field by overriding the save method in the Product model. Instructions here: http://fazle.me/auto-generating-unique-slug-in-django/
Or you could try this:
class Product(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL,
related_name='product_created', null= True,blank=True)
category = models.ForeignKey(Category, related_name='products')
name = models.CharField(max_length=200, unique=True)
slug = models.SlugField(max_length=200, unique=True)
image = models.ImageField(upload_to='products/%Y/%m/%d', blank=True)
description = models.TextField(blank=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
stock = models.PositiveIntegerField()
available = models.BooleanField(default=True)
negiotiable = models.BooleanField(default=True)
created = models.DateTimeField(auto_now_add=True)
updated = models.DateTimeField(auto_now=True)
users_like = models.ManyToManyField(settings.AUTH_USER_MODEL,
related_name='product_liked',
blank=True)
class Meta:
ordering = ('name',)
index_together = (('id', 'slug'),)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('products_detail', args=[self.slug])
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Product, self).save(*args, **kwargs)
Note that this way you will have to change db_index to unique for the name field.