Django ManyToMany add () error

I have a model that looks like this:

class PL(models.Model):
    locid = models.AutoField(primary_key=True)
    mentionedby = models.ManyToManyField(PRT)

class PRT(models.Model):
    tid = ..

The resulting in many tables in mysql is configured as

+------------------+------------+------+-----+---------+----------------+
| Field            | Type       | Null | Key | Default | Extra          |
+------------------+------------+------+-----+---------+----------------+
| id               | int(11)    | NO   | PRI | NULL    | auto_increment | 
| PL_id            | int(11)    | NO   | MUL | NULL    |                | 
| PRT_id           | bigint(64) | NO   | MUL | NULL    |                | 
+------------------+------------+------+-----+---------+----------------+

Now, if pl is an object of PL and prt, then from PRT, then execution

pl.mentionedby.add(prt)

gives me an error

Invalid integer value: "PRT object" for column "prt_id" on row 1 "

whereas

pl.mentionedby.add(prt.tid) 

works fine - with one caveat.

I can see all the elements in pl.mentionedby.all(), but I cannot go to the specified PRT object and see it prt.mentionedby_set.all().

Does anyone know why this is happening? What is the best way to fix it?

Thanks!

+3
source share
2 answers

prt . pl prt? , , Django , . , . python manage.py shell:

from yourapp.models import PL
pl = PL.objects.get(id=1)
prt = PRT.objects.get(id=1)
pl.mentionedby.add(prt)
+6

? , - - - , , .

?

0

Source: https://habr.com/ru/post/1714282/


All Articles