class User(AbstractUser):
pass
class auction_list(models.Model):
item_id=models.IntegerField(primary_key=True)
item_name=models.CharField(max_length=64)
owner=models.CharField(max_length=64)
image=models.CharField(max_length=128)
def __str__(self):
return f"{self.item_id}:{self.owner}{self.item_name}"
class bid(models.Model):
item_id=models.ForeignKey(auction_list,on_delete=models.CASCADE,related_name="currentbid")
bid=models.IntegerField(max_length=16)
user=models.CharField(max_length=64)
def __str__(self) :
return f"{self.item_id}{self.bid}{self.user}"
and when i try to inser the valules i get this error
inseritions i tried to do:
In [16]: a3=auction_list(item_id=3,item_name="keyboard",owner="alex" )
In [17]: a3.save()
In [18]: a3
Out[18]: <auction_list: 3:alexkeyboard>
In [20]: b1=bid(item_id=3,bid=100,user="alex")
and the error i get is
In [20]: b1=bid(item_id=3,bid=100,user="alex")
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-20-6befcb719aa3> in <module>
----> 1 b1=bid(item_id=3,bid=100,user="alex")
~\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\base.py in __init__(self, *args, **kwargs)
483 # checked) by the RelatedObjectDescriptor.
484 if rel_obj is not _DEFERRED:
--> 485 _setattr(self, field.name, rel_obj)
486 else:
487 if val is not _DEFERRED:
~\AppData\Local\Programs\Python\Python39\lib\site-packages\django\db\models\fields\related_descriptors.py in __set__(self, instance, value)
213 # An object must be an instance of the related class.
214 if value is not None and not isinstance(value, self.field.remote_field.model._meta.concrete_model):
--> 215 raise ValueError(
216 'Cannot assign "%r": "%s.%s" must be a "%s" instance.' % (
217 value,
ValueError: Cannot assign "3": "bid.item_id" must be a "auction_list" instance.
can some one help me understand wht is the problem here and how can i resolve these
Django automatically adds a …_id
suffix to ForeignKey
s to define a field that will refer to the primary key (or another unique field) of another model.
You thus should define your model as:
class bid(models.Model):
=models.ForeignKey(
auction_list,
on_delete=models.CASCADE,
related_name='currentbid'
)
You can then create Bid
objects with:
Bid.objects.create(
,
# …
)
or with:
Bid.objects.create(
,
# …
)
Note : Models in Django are written in PascalCase , not snake_case , so you might want to rename the model from
tobid
Bid
.
You are trying to insert an Integer when you should be inserting that Table's instance (or queryset, in other words) to the value of that item_id field in Bid Table.
What I mean is, instead of b1=bid(item_id=3,bid=100,user="alex") , you should do b1=bid(item_id=a3, bid=100, user="alex") and then .save().
If you don't want to insert the Queryset and only want to include the Integer, you can modify your code to b1=bid(item_id_item_id=3, bid=100, user="alex") and it will work just fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.