I've added a ManyToManyField to my app. Every restaurant can fall under many categories, and each category can be applied to many restaurants. The app has been working and running, and my models look like this:
class Category(models.Model):
class Meta:
verbose_name_plural = "categories"
title = models.CharField(max_length=100)
def __unicode__(self):
return self.title
class RestaurantInfo(models.Model):
name = models.CharField(max_length=100)
aboutUs = models.TextField(max_length=10000)
founded = models.DateField()
categories = models.ManyToManyField(Category)
def __unicode__(self):
return self.name
The ManyToManyFied is what is new. However, make migrations finds no changes to make.
Looks like you are not supplying app_name to makemigrations command.
python manage.py makemigrations app_name
If you won't supply app_name, Django won't detect changes made to your models.
Related
I was trying to show the list of the User's to-do lists using view.html. The error says:
no such column:testapp_todolist.user_id".
But I don't understand where this column is and how it is related to the red line in my view.html:
{% for td in user.todolist.all %}
Can you please explain in details how do I add this column?
Here's my models.py:
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class ToDoList(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name="todolist", default=0)
name = models.CharField(max_length=200)
def __str__(self):
return self.name
class Item(models.Model):
todolist = models.ForeignKey(ToDoList, on_delete=models.CASCADE)
text = models.CharField(max_length=200)
complete = models.BooleanField()
def __str__(self):
return self.text
Try solving this by writing this on your console:
python manage.py makemigrations
python manage.py migrate
Those commands are going to create tables for the TodoList model in the database.
user does not have the object todolist, todolist is a foreign key in Item. You should do:
list = ToDoList.objects.filter(user=YOUR_USER_HERE)
or
# if you want to get the todolist of the currently logged in user
list = ToDoList.objects.filter(user=request.user)
When I push my django project to heroku I get "relation "weather_city" does not exist". weather is the name of the app and city is a model.
models.py
from django.db import models
# Create your models here.
class City(models.Model):
name = models.CharField(max_length=25)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = 'cities'
I think you may have forgotten to make migrations.
heroku run bash
$ python manage.py migrate
I have made changes to one of my models in my project and migrate, makemigrations does not work as expected. Rebuilding the database creates only 2 out of 3 tables from my models.py and i cannot figure out the problem.
There are two different apps; "blog" and "users". both are registered in the setting.py.
I completely removed the database and deleted the migrations folders.
then i tried the following stuff:
django makemigrations blog
django migrate blog
doing a global django makemigrations does not have any effect, no changes are detected.
here is the relevant models.py of "blog":
class Room(models.Model):
roomname = models.CharField(max_length=6, unique=True)
roomeditors=models.ManyToManyField(User,related_name='rooms_user_can_edit', blank=True)
displayadmin=models.ForeignKey(User,
related_name='room_user_is_displayadmin',null=True, on_delete=models.SET_NULL)
def __str__(self):
return self.roomname
class Post(models.Model):
title = models.CharField(max_length=40)
content = models.TextField(max_length=300)
date_posted = models.DateTimeField(default=timezone.now)
author = models.ForeignKey(User, on_delete=models.CASCADE)
rooms = models.ManyToManyField(Room, related_name='roomposts', through='Display')
def __str__(self):
return self.title
def get_absolute_url(self):
return "/post/{}/".format(self.pk)
class Display(models.Model):
class Meta:
auto_created = True
post = models.ForeignKey(Post, on_delete=models.CASCADE)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
isdisplayed = models.BooleanField(default=0)
def __str__(self):
return str(self.isdisplayed)
every table gets created except from display. the output is:
Migrations for 'blog':
blog\migrations\0001_initial.py
- Create model Room
- Create model Post
You are giving auto_created = True in your model's Meta class, which is not recommended neither its documented. Here is the list of all possible meta options you can give inside your model.
Official documentation says:
auto_created: Boolean flag that indicates if the field was automatically created, such as the OneToOneField used by model inheritance.
Giving this in Meta refrains Django to create this model itself.
I am looking for a way to implement the "add new model_name" functionality from Django admin to normal form in templates, i.e; outside of Django admin, how could I use the same functionality.
How could I achieve it?
The first Step is to create Business Module inside models.py
class Business(models.Model):
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 = 'business'
verbose_name_plural = 'business'
def __str__(self):
return self.name
Then use python manage.py migrate to migrate module inside your database.
Now open admin.py file and register this Module,
from .models import Business
# Register your models here.
class BusinessAdmin(admin.ModelAdmin):
list_display = ['name', 'slug']
prepopulated_fields = {'slug': ('name',)}
admin.site.register(Business,BusinessAdmin)
Now check your Django admin panel. It will show you New Business Module there with Add, remove feature using Form.
I hope this will helpful for you.
This is my model code
class Poll(models.Model):
created_at = models.DateTimeField(auto_now=True)
edited_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=200,default="X vs Y")
description = models.CharField(max_length=200,default="A poll")
def __str__(self):
return self.title
class Item(models.Model):
poll = models.ForeignKey('Poll',on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now=True)
edited_at = models.DateTimeField(auto_now_add=True)
name = models.CharField(max_length=200)
type_of = models.CharField(max_length=200)
description = models.TextField(max_length=1200)
votes = models.IntegerField(default=0)
def __str__(self):
return self.name
You see votes in Item model right. That's a problem. I use makemigrations migrate command. But I still get no such column error.
Edit:
This is makemigrations vs_chart output
Migrations for 'vs_chart':
vs_chart\migrations\0001_initial.py:
- Create model Item
- Create model Poll
- Add field poll to item
This is migrate command output.
Operations to perform:
Apply all migrations: vs_chart
Running migrations:
No migrations to apply.
Before you add field poll to item, you may try to provide default value for ForeignKey, absence of default value may cause this issue:
poll = models.ForeignKey('Poll',on_delete=models.CASCADE, default=0)