Hello guys I am trying to create table in django database or in other word I am trying to syncdb but here is the error I encounter
-bash-3.2$ ~/html/ENV/bin/python2.7 manage.py syncdb
Creating tables ...
Creating table red_carpet_contactus
ValueError: The database backend does not accept 0 as a value for AutoField.
and here is the models file
from django.db import models
#from django.contrib.auth.models import User
class Page (models.Model):
Info= models.TextField()
page_name=models.CharField(max_length=100)
def __unicode__(self):
return u"%s" % self.page_name
class category(models.Model):
category_name = models.CharField(max_length=255)
def __unicode__(self):
return u"%s" %(self.category_name)
class contactus(models.Model):
PageName = models.CharField(max_length=255)
Phone = models.CharField(max_length=255)
Mob = models.CharField(max_length=255)
Address = models.CharField(max_length=255)
Fax = models.CharField(max_length=255)
Email = models.CharField(max_length=255)
def __unicode__(self):
return u"%s" %(self.page_name)
class gallery(models.Model):
title = models.CharField(max_length=200, blank=True, null=True)
Description = models.CharField(max_length=200, blank=True, null=True)
picture = models.FileField(upload_to='photos/')
thumbnail = models.FileField(upload_to='thumbnail/', blank=True, null=True)
def __unicode__(self):
return u"%s" % self.picture.url
class images(models.Model):
image = models.FileField(upload_to='images/')
#addition of the main page logo
class Main_page_img(models.Model):
logo = models.FileField(upload_to='images/')
def __unicode__(self):
return u"%s" % self.logo.url
class fields_class(models.Model):
data = models.TextField()
content = models.CharField(max_length=150)
def __unicode__(self):
return u"%s" % self.content
class trips_main_data(models.Model):
image = models.ManyToManyField(gallery)
short_description = models.CharField(max_length=255)
trip_name = models.CharField(max_length=255)
country_info = models.TextField(blank=True, null=True)
trip_info = models.TextField()
fields = models.ManyToManyField(fields_class,blank=True, null=True)
category = models.ForeignKey(category)
def __unicode__(self):
return u"%s" % self.trip_name
So would u suggest me guys to how to solve this problem??
Related
when I load the website i get this error
my models.py file looks like:
# Create your models here.
class Information(models.Model):
id = models.CharField(max_length=200, primary_key=True)
title = models.CharField(max_length=500)
link = models.CharField(max_length=100)
summary = models.CharField(max_length=1000)
published = models.CharField(max_length = 100)
def __str__(self):
return self.title
class Search(models.Model):
id = models.CharField(max_length=100, primary_key=True)
searched_titles = models.CharField(max_length=100)
searched_topics = models.CharField(max_length=100)
number_found_articles = models.IntegerField()
def __str__(self):
return self.id
class Article_search(models.Model):
id = models.AutoField(primary_key=True)
found_articles = models.ForeignKey(
Information, on_delete=models.CASCADE)
search_details = models.ForeignKey(
Search, on_delete=models.CASCADE)
def __str__(self):
return self.id
in my view.py file:
def show_articles_with_this_filter_id(request):
all = Article_search.objects.all()
print(all)
return render(request, 'show_article.html')
when I get to the print statement I get the error shown in the picture:
Unknown column 'database_article_search.found_articles_id' in 'field list'
why is the part _id pasted behind found_articles?
this is the error i get when i remove the id from article_search
id error when printing Article_search.objects.all()
# Create your models here.
class Information(models.Model):
title = models.CharField(max_length=500)
link = models.CharField(max_length=100)
summary = models.CharField(max_length=1000)
published = models.CharField(max_length = 100)
def __str__(self):
return self.title
class Search(models.Model):
searched_titles = models.CharField(max_length=100)
searched_topics = models.CharField(max_length=100)
number_found_articles = models.IntegerField()
def __str__(self):
return self.searched_titles
class Article_search(models.Model):
found_articles = models.ForeignKey(
Information, on_delete=models.CASCADE)
search_details = models.ForeignKey(
Search, on_delete=models.CASCADE)
Make model like this and then run two commands
1.python manage.py makemigrations and then 2.python manage.py migrate
"In your views.py file"
def show_articles_with_this_filter_id(request,id):
all = Article_search.objects.filter(id=id)
print(all)
return render(request, 'show_article.html')
hello am just wondering why cant I return return f'{self.user.username} Profile'
form this model
class School(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
region = models.CharField(max_length=200)
district = models.CharField(max_length=200)
code = models.CharField(max_length=20)
logo = models.ImageField(default='default.jpg', upload_to='logos')
def __str__(self):
return f'{self.user.username} Profile'
what should be the syntax here help
You are referencing int instead of User object. Int doesn't have field username. If you want to create Many-to-one relation in Django you should use `ForeignKey. Ref: https://docs.djangoproject.com/en/3.1/ref/models/fields/#django.db.models.ForeignKey
Change your class to:
class School(models.Model):
user = models.ForeignKey(User, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=200)
region = models.CharField(max_length=200)
district = models.CharField(max_length=200)
code = models.CharField(max_length=20)
logo = models.ImageField(default='default.jpg', upload_to='logos')
def __str__(self):
return f'{self.user.username} Profile'
I'm trying to create a model for an eCommerce site but after makemigrations when I trying to migrate terminal show "No migration to apply" but when I checked database no new table was there. Please help me.
`
from django.db import models
from django.utils import timezone
# Create your models here.
class Collection(models.Model):
name = models.CharField(max_length=200, null=True)
def __str__(self):
return self.name
class ProductCategory(models.Model):
name = models.CharField(max_length=100, null=True)
def __str__(self):
return self.name
class Brand(models.Model):
name = models.CharField(max_length=100, null=True)
def __str__(self):
return self.name
class Color(models.Model):
color = models.CharField(max_length=50, null=True)
def __str__(self):
return self.color
class Size(models.Model):
size = models.CharField(max_length=20, null=True)
def __str__(self):
return self.size
class Products(models.Model):
name = models.CharField(max_length=200, null=True)
for_people = models.ForeignKey(Collection, on_delete=models.CASCADE)
category = models.ForeignKey(ProductCategory, on_delete=models.CASCADE)
description = models.TextField()
old_price = models.FloatField(null=True, blank=True)
price = models.FloatField(null=True)
input_date = models.DateTimeField(auto_now=False, auto_now_add=True)
update_date = models.DateTimeField(auto_now=True, auto_now_add=False)
def __str__(self):
return self.name
class Image(models.Model):
product_name = models.ForeignKey(Products, related_name='pro_images', on_delete=models.CASCADE)
image = models.ImageField(upload_to='media/product_img/', null=True, blank=True)
def __str__(self):
return self.product_name.name + 'image'
`
The below picture of my terminal please check it
Terminal show
python manage.py migrate product
After migrate with the specific model name it's worked for me.
I'm trying to save an Item based on the Foreign Key Primary Key for topCategory and middleCategory.
models.py
class MiddleCategory(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField()
def __str__(self):
return self.title
class TopCategory(models.Model):
title = models.CharField(max_length=50)
slug = models.SlugField()
middleCategory = models.ManyToManyField(MiddleCategory)
def __str__(self):
return self.title
# Item
class Product(models.Model):
title = models.CharField(max_length=500, db_index=True)
price = models.DecimalField(max_digits=10, decimal_places=2)
brand = models.CharField(max_length=100)
# brand_id = models.IntegerField()
retailer = models.CharField(max_length=255)
image = models.CharField(max_length=1000)
url = models.URLField(max_length=800, unique=True, default='')
model_numbers = models.BigIntegerField(default=0)
description = models.CharField(max_length=500)
featured = models.CharField(max_length=255, db_index=True, default='NO')
timestamp = models.DateTimeField(auto_now=True)
topCategory = models.ForeignKey(TopCategory)
middleCategory = models.ForeignKey(MiddleCategory)
def __str__(self):
return self.title
In my script I'm saving like this:
Product(title=title,
price=price,
brand=brand,
retailer=retailer,
image=image,
url=url,
description=description,
midCategory=midCategory,
topCategory=topCategory
).save()
This is the error I am getting:
ValueError: Cannot assign "'1'": "Product.topCategory" must be a "TopCategory" instance.
You can get instance of TopCategory
instance = TopCategory.objects.get(id=topCategory)
then
Product(title=title,
price=price,
brand=brand,
retailer=retailer,
image=image,
url=url,
description=description,
midCategory=midCategory,
topCategory=instance
).save()
I have a simple model, witch is used as a form .
class Test(models.Model):
name = models.CharField(max_length=100, unique=True, db_index=True)
location = models.CharField(max_length=300)
details = models.TextField()
def __unicode__(self):
return self.image.name
I would like to add the following class Album as a foreign key to Test :
class Album(models.Model):
title = models.CharField(max_length=60)
public = models.BooleanField(default=False)
def __unicode__(self):
return self.title
class Tag(models.Model):
tag = models.CharField(max_length=50)
def __unicode__(self):
return self.tag
class Image(models.Model):
title = models.CharField(max_length=60, blank=True, null=True)
image = models.FileField(upload_to="images/")
tags = models.ManyToManyField(Tag, blank=True)
albums = models.ManyToManyField(Album, blank=True)
created = models.DateTimeField(auto_now_add=True)
rating = models.IntegerField(default=50)
width = models.IntegerField(blank=True, null=True)
height = models.IntegerField(blank=True, null=True)
user = models.ForeignKey(User, null=True, blank=True)
def __unicode__(self):
return self.image.name
Questions:
How to add class Album as a foreigh key to class Test?
How to put this relation on the form? - e.g. user is selecting multiple images for uploads wich results in unique Album related to Test class.
Do you mean something like this for the foreign-key
class Test(models.Model):
name = models.CharField(max_length=100, unique=True, db_index=True)
location = models.CharField(max_length=300)
details = models.TextField()
album = models.ForeignKey(Album, null=True, blank=True)
def __unicode__(self):
return self.name