2đź‘Ť
You need to use keyword arguments when you create new records in your Work table.
homework = Work(title = "homework", amount = 12, drop = 15)
homework.save()
The order in which you specify the arguments will not matter. If keywords for a function was specified something like
def attributes(self, pk='', title='', amount=''):
...
Then you could specify the arguments in order without the keyword. However, Django models don’t do it this way because they can’t guess what the names of your fields will be. They would implement much more like this
def attributes(self, **kwargs):
...
Where kwargs will be a dictionary of all keyword arguments specified. Django models will then update the db based on any keyword arguments you pass in there.
0đź‘Ť
It’s not that the first parameter should be an int. It should be a charfield, with the content of a number. Django is trying to convert it to an int, but as the text is homework
– which is not a valid number, it fails. The first argument should be id
, an integer, or a string of number.
You have to specify the keywords :
homework = Work(title = "homework", amount = 12, drop = 15)
Why? Because the default first argument is not the title or anything user-defined, it’s the id. So, if you want to skip the id (which will be set automatically by Django), and probably a few other generated arguments, you’ll have to specify the keywords.
- [Answered ]-How to set django many-to-many field to accept null
- [Answered ]-Tracking user web search history in Django
- [Answered ]-With Django ModelForms, how do I including the "id" column or primary key in the HTML?
0đź‘Ť
Every django model has a auto-primary-key, and its default name is id
. When initializing the Work model with Work(“homework”,12,15), you are passing the “homework” string to the id field.
from product.models import Work
homework = Work("homework",12,15)
print homework.id
#outputs 'homework'
I also can not find any words in the django doc that guarantee the order of Model.__init__
args is as same as the order of field definitions(because of the metaclass, the orders could be different). Thus you should use name parameters(kwargs).
- [Answered ]-Django admin, change current value of ForeignKey widget
- [Answered ]-How could I make tagged user in the comment a hyperlink to that user's home page? (Django)
- [Answered ]-Loading css and javascript files in more then one django templates without having to to re load css and js
0đź‘Ť
You can achieve this by simply
Work.objects.create(title="homework", amount=12, drop=15)
- [Answered ]-Django Does Not Write Logs to File
- [Answered ]-Django access m2m field attribute from a queryset
- [Answered ]-Django User model customize validation
- [Answered ]-Django 500 Internal Server Error – ImproperlyConfigured: Error loading MySQLdb module:
- [Answered ]-How to make Django admin shows OneToMany instead of ForeignKey