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
Related
I use mysql to test model.
With django_comment_migrate helps, help_text saved to sql table field comment.
Original model.py below:
from django.db import models
class Foo(models.Model):
name = models.CharField(max_length=20, verbose_name='alias_name', help_text='alias_name')
Run python manage.py inspectdb got result below, lost help_text.
class App1Foo(models.Model):
id = models.BigAutoField(primary_key=True)
name = models.CharField(max_length=20)
class Meta:
managed = False
db_table = 'app1_foo'
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)
I am working on a Django project, and had registered all my models to admin. But due to some reason, I cannot change any of the instances of any model that I created.
This is how it shows:
This is the initial screen as I log in.
As soon as I click on add/change, instead of the usual screen it shows like this:
Here are my models.py and admin.py:
admin.py:
from django.contrib import admin
from .models import Plant, Unit, Equipment, EquipmentCategories, Job, Task_category, Subtask_category, SubtaskStatus, TaskStatus, Files, Filetype, Comment, Timelog, Report, Approvals, Task_flow, Subtask_flow
# Register your models here.
admin.site.register(Plant)
admin.site.register(Unit)
admin.site.register(EquipmentCategories)
admin.site.register(Equipment)
admin.site.register(Job)
admin.site.register(Task_category)
admin.site.register(TaskStatus)
admin.site.register(Subtask_category)
admin.site.register(SubtaskStatus)
admin.site.register(Files)
models.py:
from django.db import models
from django.contrib.auth.models import User
import django.dispatch
# Create your models here.
class Plant(models.Model):
name = models.CharField(max_length=200)
start_date = models.DateField(null=True,blank=True)
end_date = models.DateField(null=True,blank=True)
def __str__(self):
return(self.name)
class Unit(models.Model):
plant = models.ForeignKey(Plant,related_name='Plant', on_delete=models.CASCADE)
name = models.CharField(max_length=200)
unit_location = models.CharField(max_length=200, null=True)
start_date = models.DateField(null=True,blank=True)
end_date = models.DateField(null=True,blank=True)
def __str__(self):
return(self.name)
class EquipmentCategories(models.Model):
unit = models.ForeignKey(Unit,related_name='Unit', on_delete=models.CASCADE)
name = models.CharField(max_length=200)
def __str__(self):
return(self.name)
Can anyone please help!
Thanks in advance :)
I think something is wrong with your STATIC_URL in your settings.py file.
That fixed the issue for me.
It's not finding the right path to your static/admin css and js files.
This problem also occurs when you don't migrate your models properly in the terminal. Make sure to always migrate your models after having modified them.
Will be an issue with your user permissions.
Open the command line and cd /to/your/directory/with/manage.py and run python -m manage createsuperuser
Once you've created that login with those details and you should be able to update things.
I have created two classes in models.py in my application.
models.py
from django.db import models
# Create your models here.
class User(models.Model):
first_name = models.CharField(max_length=264, unique=True)
last_name = models.CharField(max_length=264, unique=True)
email = models.CharField(max_length=264,unique=True)
def __str__(self):
return self.first_name
class NewUser(models.Model):
categorie = models.ForeignKey('User',on_delete=models.DO_NOTHING)
area = models.CharField(max_length=264)
def __str__(self):
return self.name
as shown in my image my (User and New users)tables are created.
data is getting added to my (User) table.
But when I try to add data to my (New users) table
I get this error
Since you don't have any custom fields in your User model, you dont need to create a seperate User class, only you have to import the built in User class.
from django.contrib.auth.models import User
class NewUser(models.Model):
categorie = models.ForeignKey('User',on_delete=models.DO_NOTHING)
area = models.CharField(max_length=264)
def __str__(self):
return self.categorie.user.username
You can still get username, first_name, last_name, email etc from the default user class. Refer: https://docs.djangoproject.com/en/3.0/ref/contrib/auth/#django.contrib.auth.models.User
Most likely you haven't migrated properly. Try:
python manage.py makemigrations
python manage.py migrate
The models show up in the admin because they are present in the apps models.py. This is not related to the database!
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.