9👍
The problem was in migration files. I just opened and changed default value from string type to integer.
15👍
Just to share the solution that worked with my similar error that been received:
In my case, this same error was received because I was creating the model instant with the fields values directly, without specifying the field name, so always the ID was taking the first one as default (ID=field1).
The problem solved by adding the attributes name as well to the Instant creation.
Was:
model_instant = YourModel(field1, field2,...etc)
Solved by:
model_instant = YourModel(field1 = field1, field2 = field2,...etc)
Do this then follow what been recommended above of 1) deleting the dB file, and 2) delete the migrations then 3) do the makemigrations your_app_name then 4) migrations, then 5) run the server and you should be good to go.
- Django, REST and Angular Routes
- ModelViewSet – Update nested field
- No Such Column Error in Django App After South Migration
- Django File Upload and Rename
10👍
Just delete all the migration files except the init python file the run python manage.py makemigrations then python manage.py migrate
- What is Serializers to_internal_value method used for in Django
- Django.db.utils.OperationalError: (1045, "Access denied for user 'root'@'localhost' (using password: YES)")
- Django admin.TabularInline auto insert three blank row
- Django.core.exceptions.FieldDoesNotExist: model has no field named <function SET_NULL at 0x7fc5ae8836e0>
7👍
2👍
I was getting a similar error and finally found the cause. Just sharing here in case someone made a similar mistake.
I had defined a custom __init__
method on my model and had by mistake only written one *
when passing kwargs to super
:
def __init__(self, *args, **kwargs):
super().init(*args, *kwargs)
This resulted in the error message:
ValueError: Field 'id' expected a number but got 'account'.
account
was the first keyword argument I was passing when creating a new instance in a view.
In hindsight, the error message makes sense, but it took me an hour of confused debugging before I noticed my typo.
- Django: values_list() multiple fields concatenated
- Django on Heroku – Broken Admin Static Files
- Docker compose for production and development
- When trying set corsheaders in settings.py file
- Python venv not creating virtual environment
1👍
I got this error when I never added created
to the get_or_create
someVariable = "someVariable"
# This will give the error "ValueError: Field 'id' expected a number but got someVariable"
my_model = myModel.objects.get_or_create(someField=someVariable)
It got solved when adding this
# added created
my_model, created = myModel.objects.get_or_create(someField=someVariable)
See this https://docs.djangoproject.com/en/4.0/ref/models/querysets/#get-or-create
0👍
I had the same problem with simple CBV like TemplateView or ListView which does not require mandatory parameter. I’m pretty sure the issue comes from the url interpretation. For a simple ListView like
class ProfileList(generic.ListView):
model = get_user_model()
The url
path('profile_list/dummy', ProfileList.as_view(), name='profile_lv'),
works, whereas the one below, doesn’t, the error: Field ‘id’ expected a number but got ‘profile_lv’ is thrown. Where profile_lv is the name of the url…
path('profile_list', ProfileList.as_view(), name='profile_lv'),
The addition of anything with a slash(/) after the path works?!…
- Determine if an attribute is a `DeferredAttribute` in django
- UUID field added after data already in database. Is there any way to populate the UUID field for existing data?
- How to test (using unittest) the HTML output of a Django view?
0👍
Just do nothing There are two ways :
1)Delete Your all migrations and type in your terminal:python3 manage.py makemigrations <app_name>
then python3 manage.py migrate
and then runserver.
2)Just opened and changed default value from string type to integer.
In my opinion choose first option because it is safe way.
Thanks
- Django form with ManyToMany field with 500,000 objects times out
- Django queryset order_by dates near today
- Is there a way to generate pdf containing non-ascii symbols with pisa from django template?
- Django + Forms: Dynamic choices for ChoiceField
0👍
Please Delete your recentally created migrations file then run python manage.py makemigrations and python manage.py migrate. I think your problem going to solve.Please try it now.
- Django CharField without empty strings
- Django test: TransactionManagementError: You can't execute queries until the end of the 'atomic' block
- How to embed matplotlib graph in Django webpage?
- Django: values_list() multiple fields concatenated
0👍
Go to migration files and find the name id is where generate, then just replace id with processor or other name or remove it as it define as verbose_name.
`python manage.py makemigrations`
`python manage.py migrate`
`python manage.py runserver`
- Django and Celery – ModuleNotFoundError: No module named 'celery.task'
- Creating a .gitignore file for a Django website
- Maintain SQL operator precedence when constructing Q objects in Django
- Django querysets + memcached: best practices
0👍
Unfortunately, I was faced with this problem, but I was forced to create a new Django project from first, to solve this problem. And it was not solved by just deleting migrations files.
- Django: How to add an extra form to a formset after it has been constructed?
- List filter by custom list display field in django admin
- Error Map Keys Must be unique while using YAML extension field in docker compose for django application
- Django 3.1: StreamingHttpResponse with an async generator
0👍
I have confronted with similar type of error. My original URL was this one:
v1/unit/
Cleaning the migration files didn’t help me. Fortunately, It just started to work after extending the URL with an additional string:
v1/unit/addProduct/
- TypeError: cannot unpack non-iterable int object in Django views function
- Remove non-ASCII characters from a string using python / django
- Django Template Not Found
- How does Django serve media files?
- Django testing: Got an error creating the test database: database "database_name" already exists
0👍
You can do the following:
-
Clean up your dbase by deleting entries
which do not confirm to your model,or putting in values for missing/wrong entries
-
Make sure values in models are
blank=False, null=False
anddefaults given
and then rerun the migration -
Put a condition such as if
Table.field != "":
or whatever value is causing problem in your serializers
- Refresh <div> element generated by a django template
- Django HTML E-mail template doesn't load css in the e-mail
- Does a library to prevent duplicate form submissions exist for django?
- How to filter objects by ignoring upper and lower case letter django
- Google.maps.event.addDomListener() is deprecated, use the standard addEventListener() method instead : Google Place autocomplete Error
0👍
I got the similar error below:
TypeError: Field ‘id’ expected a number but got <Country: USA>.
Because get_country()
returns an object to default instead of id
as shown below:
# "models.py"
class Country(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
@classmethod
def get_country(cls): # ↓ ↓ ↓ An object is returned ↓ ↓ ↓
return Country.objects.get_or_create(id=1, name="USA")[0]
class Shipping(models.Model):
country = models.ForeignKey(
Country, # ↓ An object is returned
default=Country.get_country,
on_delete=models.CASCADE
)
fee = models.DecimalField(max_digits=5, decimal_places=2)
def __str__(self):
return str(self.country)
So, I returned id
from get_country()
as shown below, then the error was solved:
# "models.py"
class Country(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return self.name
@classmethod
def get_country(cls): # ↓ Here
return Country.objects.get_or_create(id=1, name="USA")[0].id
Be careful, even if changing self.name
to str(self.id)
as shown below:
# "models.py"
class Country(models.Model):
name = models.CharField(max_length=20)
def __str__(self):
return str(self.id) # Here
@classmethod
def get_country(cls):
return Country.objects.get_or_create(id=1, name="USA")[0]
# ...
Or, even if removing __str__()
as shown below:
# "models.py"
class Country(models.Model):
name = models.CharField(max_length=20)
# def __str__(self):
# return str(self.id)
@classmethod
def get_country(cls):
return Country.objects.get_or_create(id=1, name="USA")[0]
# ...
Then, the error was not solved.
-3👍
I solved it by deleting the db
, migrations
and pycache
files then run migrations
& migrate
again.
- Create a new model which have all fields of currently existing model
- Add functionality to Django FlatPages without changing the original Django App
- Get ContentType id in Django for generic relation
- "Returning to that page might cause any action you took to be repeated" – Django