Invalid pk \"1\" - object does not exist.

863 views
Skip to first unread message

optvi...@gmail.com

unread,
Feb 25, 2019, 8:36:05 AM2/25/19
to Django users
getting error


{"manager":["Invalid pk \"1\" - object does not exist."]}


here is my model class

class Employee(models.Model):

MANAGER = 'MGR'
STANDARD = 'STD'


EMPLOYEE_TYPES = (

(MANAGER, 'manager'),
(STANDARD, 'employee')

)


role = models.CharField(max_length=25, choices=EMPLOYEE_TYPES)
name = models.CharField(max_length=100, null=True, blank=True)
employee_id = models.CharField(max_length=100, null=True, blank=True)
manager = models.ForeignKey('self', null=True, related_name='employee',on_delete=models.CASCADE)


and json data which i am using to save in table

{
"role":"MGR",
"name":"John",
"employee_id" :"IN",
"manager":1

}




Phako Perez

unread,
Feb 25, 2019, 5:44:53 PM2/25/19
to django...@googlegroups.com
I guess you need to define first the id in models.CASCADE, 

When you try to insert as per your table definition it looks for the key on that table

manager = models.ForeignKey('self', null=True, related_name='employee',on_delete=models.CASCADE)
Hope this can be helpful 

Happy coding!!!
Sent from my iPhone
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/454e6708-d6f6-43ee-8542-85d92a69d1cd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

opt visitor

unread,
Feb 25, 2019, 8:32:08 PM2/25/19
to django...@googlegroups.com
Can you please explain how to define I'd in model.Cascade

To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

anthony....@gmail.com

unread,
Feb 26, 2019, 5:02:42 AM2/26/19
to Django users
Hello,

What if you use kind of the following as json data:
nb: The following model is a simplified version with only two fields (name and manager which is a FK to itself such as in yours).

[
  {
    "model": "YourApp.YourModelName",
    "pk": 1,
    "fields": {
      "name": "toto",
      "manager": null
    }
  },
  {
    "model": "YourApp.YourModelName",
    "pk": 2,
    "fields": {
      "name": "tata",
      "manager": 1
    }
  }
]

In that case you ensure that a manager object with pk equals to 1 already exists and therefore can be referred to .

'Hope it helps.

ons dridi

unread,
Feb 26, 2019, 6:27:53 AM2/26/19
to django...@googlegroups.com
Please ; can i any one explane for me what is django and what is the relation between it any the database ? please :(

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Kasper Laudrup

unread,
Feb 26, 2019, 7:01:27 AM2/26/19
to django...@googlegroups.com
Hi Ons Dridi,

On 2/26/19 12:24 PM, ons dridi wrote:
> Please ; can i any one explane for me what is django and what is the
> relation between it any the database ? please :(
>

Have a look here:

https://www.djangoproject.com/

Also, please don't highjack threads with unrelated questions.

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages