1

I am learning django and I am working on models. I created a Product class

from django.db import models


class Product(models.Model):
    title = models.CharField(max_length=10)
    description = models.TextField(blank=False)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    featured = models.BooleanField(default=False)
    int = models.IntegerField(default=10)

and I am trying to delete the 'int' field but the django site does not update after the chanage.

After deleting a model field(the 'int' field, see the screenshots), and running

python manage.py makemigrations

python manage.py migrate

the live preview does not update and throws an error regarding the erased field. after closing the server and restarting the server(ctrl + c and than python manage.py runserver the page updates and does not throw any errors

I am using sqlite. I tried deleting 'pycache' and all the migrations also db.sql3, recreating the superuser but after removing any field I still get the same error.

picture 1 -> initial state $ https://ibb.co/Yb6pmgr picture 2 -> after removing 'int' field https://ibb.co/xf1bJQJ picture 3 -> error : $ https://ibb.co/0cPyRZv

arajshree
  • 626
  • 4
  • 13
nickk2002
  • 49
  • 1
  • 6
  • Not really clear what you are saying, can you please edit your post and make it easier to read? – Arnav Poddar Jun 24 '19 at 05:57
  • 1
    @ArnavPoddar I edited the question, hope is clear now – nickk2002 Jun 24 '19 at 06:08
  • If you have the data that you're trying to add into the table backed up somewhere why don't you try dropping the table completely and deleting all migrations and then make migrations again? – Sharpfawkes Jun 24 '19 at 06:12
  • @LeroyJD already done that but it is annoying to this everytime. delete migrations. crreate a super user etc. just for one change.. – nickk2002 Jun 24 '19 at 06:49
  • You do not have to create the super user everytime, run migrations on your app name alone as python manage.py makemigrations that way it won't try to make changes to the auth_user tables and your SU still exists. Won't solve your problem but should save you some time in the future :). – Sharpfawkes Jun 24 '19 at 06:59
  • Can you put up the code that is in the migrations file that was created when you deleted the field? – Sharpfawkes Jun 24 '19 at 07:05

1 Answers1

1

First stop your server, then do your changes and run python manage.py makemigrations and python manage.py migrate commands. Maybe this will work, but you are playing with reserved keyword in Python.

If it's not working, for now just remove the 0001_initial.py which is placed on trydjango/src/products/migrations/ path. If you already have some data in your database please backup somewhere. int is one of the Built-in Functions in Python. Please don't such a naming convention every where in Python. Check the below example, you will get some idea about it.

str_val = '1'

print(str_val)  # 1
print(type(str_val))  # <class 'str'>

int_val = int(str_val)

print(int_val)  # 1
print(type(int_val))  # <class 'int'>

It is just a simple example of convert string value into integer value using int(). Let's assign some value to int().

# by default
print(int)  # <class 'int'>
print(type(int))  # <class 'type'>

# let's get the previous example again
str_val = '1'

print(str_val)  # 1
print(type(str_val))  # <class 'str'>

# assign some value to int(). (please please please never do such a thing like this)
int = 'assigned string'
print(int)  # 'assigned string'
print(type(int))  # <class 'str'>

int_val = int(str_val)

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

You'll get an error like that when you execute above code. So int() will not convert string to integers further more. Because you broke the rule. Now it's just like a normal variable. Still if you really want to add a variable like int, just use _int or int_ and if you want to display column name as int just add the db_column name.

_int = models.IntegerField(default=10, db_column='int')

# or

int_ = models.IntegerField(default=10, db_column='int')

Before you do these changes please stop your server. Then edit, after that run python manage.py makemigrations and python manage.py migrate. I hope this answer will help you somehow.

UPDATE :

This answer will help you to find all the reserved keyword in Python and don't use any of these as your variable names.

Kushan Gunasekera
  • 7,268
  • 6
  • 44
  • 58
  • thank you it worked! I have learned my lesson and will not use 'int' as var name in the futere – nickk2002 Jun 24 '19 at 07:54
  • You're welcome @nickk2002, for more information read this [article](https://reinout.vanrees.org/weblog/2014/12/12/naming_things.html) and this [answer](https://stackoverflow.com/a/22864250/6194097) will help you to find all the reserved keyword in Python. – Kushan Gunasekera Jun 24 '19 at 08:18