I'm pulling my hair out with this. When I attempt to add a 'Product' from my admin page, I'm getting an IntegrityError: main_product.img may not be NULL.
Models.py
class Product(models.Model):
name = models.CharField(max_length=500)
short = models.CharField(max_length=250)
description = models.TextField()
price = models.DecimalField(max_digits=8, decimal_places=2)
in_stock = models.BooleanField()
def __unicode__(self):
return self.name
class ProductImages(models.Model):
product = models.ForeignKey(Product, blank=True)
img = models.ImageField(upload_to='images', blank=True)
caption = models.CharField(max_length=300, blank=True)
class ProductFeatures(models.Model):
product = models.ForeignKey(Product)
feature = models.CharField(max_length=500)
Admin.py
class ProductFeaturesAdmin(admin.TabularInline):
model = ProductFeatures
extra = 1
class ProductImageAdmin(admin.TabularInline):
model = ProductImages
extra = 1
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'in_stock')
inlines = [ProductFeaturesAdmin, ProductImageAdmin]
admin.site.register(Product,ProductAdmin)
I was using Pillow to resize images when being upload, so I had a custom save() function in my ProductImages model. I removed that thinking it was the problem, but it's still not working. As you can tell, I'm really new to Django and Python. Any and all help appreciated.
Edit: forgot to mention that I've added blank=true and null=true to Product.img, then used South to migrate the table.
Edit 2: This is my new ProductImages model.
class ProductImages(models.Model):
product = models.ForeignKey(Product)
img = models.ImageField(upload_to='images', blank=True, null=True)
caption = models.CharField(max_length=300, blank=True)
I used South and ran:
python manage.py schemamigration main --auto
python manage.py migrate main
Still having the error. If I were to add the img field to my Products model, how would I be able to add multiple imgs in the admin panel?
Actually you need to add null=True in the img field, So make it img = models.ImageField(upload_to='images', blank=True, null=True).
The difference is that because blank=True decides that whether the fields will be required by the forms. blank=False means that field cannot be blank and blank=True means field will not be required.
Regarding null=True sets NULL in column of your DB which will solve your issue.
Related
I created a Projects model
class Projects(models.Model):
name = models.CharField(max_length=257, unique=True, null=False)
description = models.TextField()
active_issue_count = models.IntegerField(default=0) # functiona bağlanmalı
solved_issue_count = models.IntegerField(default=0) # functiona bağlanmalı
is_active = models.BooleanField()
start_date = models.DateTimeField()
deadline = models.DateTimeField()
and I want to use this project model Projects.objects.all() with this code but when I typed in pyCharm shows me this suggestion
but while I try to use this model
class User(AbstractUser, PermissionsMixin):
objects = UserManager()
related_group = models.CharField
current_project = models.ForeignKey(to='core.Project',
related_name='current_project', on_delete=models.PROTECT, null=True)
total_worked_project = models.IntegerField(default=0) # functiona bağla
active_work_project_count = models.IntegerField(default=0) # functiona bağla
REQUIRED_FIELDS = ['email']
`
I can use User.objects.all() without suggestion what should I do anyone can help me ?
PyCharm just don't follow some object's relation. Here it has no clue that this Model class has related Manager, that you can call with objects.
You can ignore it in PyCharm so it will not bother you.
More actions... => Ignore (...)
I'm having an issue with the table names format of my Django project.
Here is a sample of a model I have:
class WalletHistory(models.Model):
wallet = models.ForeignKey(Wallet, on_delete=models.CASCADE, related_name='wallet_history')
free_amount = AmountField(default=None, blank=True, null=True)
locked_amount = AmountField(default=None, blank=True, null=True)
flexible_amount = AmountField(default=None, blank=True, null=True)
date_created = models.DateTimeField(auto_now_add=True, blank=True)
The app associated with this model is called core so the table name is supposed to be core_wallet_history. But Django names my table core_wallethistory instead: it doesn't split the class names with underscore.
I've changed nothing in the Django settings.
Sorry I can't post comments yet as I don't have enough reputation.
How are rendering your tables?
I use django-tables2 and all I have to change is the attributes via the class Meta under the table.
Example:
class offer_table(ExportMixin,tables.Table):
offer_name = tables.Column(accessor='offer_name')
class Meta:
attrs = {"name":"offer_table", "id":"offer_table",}
I'm trying to perform a reversed query for a manytomany fields in Django, but it keeps gives me nothing, here is my code
models.py
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=120)
image = models.ImageField(upload_to='products')
branch = models.ManyToManyField(Branch, related_name='branches')
class Branch(models.Model):
area = models.ForeignKey(Area, on_delete=CASCADE)
name = models.CharField(max_length=1200)
phone = models.CharField(max_length=30, null=True, blank=True)
address = models.CharField(max_length=1200, null=True, blank=True)
tax_value = models.DecimalField(decimal_places=2, max_digits=4)
views.py
for branch in product_object.branches.all():
print(branch)
The branch is always nothing !!
For some reason, the related name is not calling it anymore. I called it using the model name (lower cased).
This is how it worked
for branch in product_object.branch.all():
Just to complete your answer above, I think the way you have your model set up is a little misleading and confusing. I think this would improve clarity:
class Product(models.Model):
category = models.ForeignKey(Category, on_delete=models.CASCADE)
name = models.CharField(max_length=120)
image = models.ImageField(upload_to='products')
branches = models.ManyToManyField(Branch, related_name='products')
Since you have a many to many field, a product can have multiple branches, the attribute name should reflect that
When you use the related_name, this would be if you are going from the m2m object. For example, if you have a branch, you could get all it's products by doing branch.products
I am trying to create a cart model using ForeignKey relation with User.
My mall/models.py:
from django.db import models
from django.contrib.auth.models import User
class products(models.Model):
image = models.ImageField(upload_to='products/', blank=True)
name = models.CharField(max_length=100)
detail = models.TextField(max_length=100, verbose_name='detail of product')
price = models.FloatField()
def __str__(self):
return self.name
class cart(models.Model):
item = models.OneToOneField(products, on_delete=models.CASCADE)
user = models.ForeignKey(User, null=True, blank=True, default=None, on_delete=models.CASCADE)
def __str__(self):
return self.item.name
I am getting a error like this:
django.db.utils.IntegrityError: NOT NULL constraint failed: new__mall_cart.user_id
whenever I am trying to migrate it. Its showing the same error if I am migrating the products class alone as well
.
I have provided null=True as well as blank=True and the superuser is created with the name of admin. What is issue here?
I was wondering if it's possible to arrange pictures uploaded to wagtail into galleries based on their tags.
I have an album app, and its models.py contains :
from django.db import models
from wagtail.wagtailcore.models import Page
# Create your models here.
class Category(Page):
name = models.CharField(max_length=250)
slug = models.SlugField()
image = models.ImageField()
parent = models.ForeignKey('self', blank=True, null=True, related_name='children')
def __str__(self):
return self.name
class Post(Page):
title = models.CharField(max_length=120)
category = models.ForeignKey('Category', null=True, blank=True)
publish = models.DateField(auto_now=False, auto_now_add=False, )
slug = models.SlugField(unique=True)
Images use django-taggit for its tags so you can query them easily.
from wagtail.wagtailimages import get_image_model
# Somewhere in your code
image_class = get_image_model()
images = image_class.objects.filter(tags__name='the_slug')
On a side note, the slug field on your Category page in your snippet might shadow the slug field of the base Page model so you might want to be careful with that.