When all is as usual, you don't play with pk in your code.  You put the
foreign object in the field of the referring object.  (In python that's
just a reference, so don't worry about copies, etc.)  When the referring
object is saved, django saves the pk of the object in the field in the db
in a field called foo_id, if the field itself is called foo. Later when
you fetch the referrer, say as bar, then when you refer to bar,foo django
makes sure the foo object has been fetched, so bar.foo.ugh get's you the
ugh field of the foo object referred to by bar.

I suggest that with this in mind, you go back of the part of the tutorial
example that talks about foreign keys and see how many of the things that
you're doing aren't required, and are just opportunities to confuse things.

On Mon, Mar 5, 2018 at 6:21 PM, Malik Rumi <malik.a.r...@gmail.com> wrote:

> I tried the to_field argument, that was a mistake, and I put it back.
>
> I was just inserting the pk of the foreign object. However, as things are
> moving through the pipeline, I used uuid.uuid4() to create additional pks
> as needed at it went along.
>
> Example:
>
>     def process_item(self, item, spiders):
>         itemcasebycase['case_arrow'] = item['uniqid']
>         itemcasebycase['uniqid'] = get_u
>
> But this is what works in the pipeline:
>
>         scotus = Jurisdiction.objects.get(
>             uniqid='5e4dd0c2-5cc9-4d08-ab43-fcf0e84dfc44')
>         item['jurisdiction'] = scotus
>
> And this is the error message I got:
>
> ValueError: Cannot assign "UUID('5e4dd0c2-5cc9-4d08-ab43-fcf0e84dfc44')":
> "Case.jurisdiction" must be a "Jurisdiction" instance
>
> Finally, yes, under normal circumstances, my pk uuids are made
> automatically. Thanks
>
> *“None of you has faith until he loves for his brother or his neighbor
> what he loves for himself.”*
>
> On Mon, Mar 5, 2018 at 2:16 PM, Bill Freeman <ke1g...@gmail.com> wrote:
>
>> Are you specifying the to_field argument, or are you letting it default?
>>
>> And is the pk of the other model made by Django by default, or are you
>> explicitly specifying a foreign key constraint on some field of your own.
>>
>> Things might be better in 2.0, but I've had my troubles with pk that
>> isn't an AutoField (such as made by default).
>>
>> If you're only doing the standard (defaulted) stuff, it's a mystery.
>> Either way folks probably need to see some of your code to help out.
>>
>> On Mon, Mar 5, 2018 at 5:06 PM, Malik Rumi <malik.a.r...@gmail.com>
>> wrote:
>>
>>> Hello all,
>>>
>>> Up to now, (admittedly not long - I'm not a total newbie but I still
>>> have a LOT to learn) when making a foreign key I would just put the pk of
>>> that instance in the fk field, as the docs suggest:
>>>
>>> By default ForeignKey will target the pk of the remote model but this 
>>> behavior
>>> can be changed by using the ``to_field`` argument.
>>>
>>> *https://docs.djangoproject.com/en/2.0/_modules/django/db/models/fields/related/#ForeignKey
>>> <https://docs.djangoproject.com/en/2.0/_modules/django/db/models/fields/related/#ForeignKey>
>>> *
>>>
>>> However, recently while working with a scrapy pipeline, these fields
>>> started catching errors, which told me
>>>
>>> "...must be an instance of ...(foreign object)"
>>>
>>> And sure enough, if I did a get queryset for the one specific instance
>>> in question, that worked. My question is why, or maybe it should be, what's
>>> the difference, or maybe even, what's going on here, or wtf?
>>>
>>> Any light you can shed on this for me would, as always, be greatly
>>> appreciated.
>>>
>>> --
>>> 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+unsubscr...@googlegroups.com.
>>> To post to this group, send email to django-users@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/ms
>>> gid/django-users/5f9399d4-6a48-450a-b2bd-2e33a296b1b5%40googlegroups.com
>>> <https://groups.google.com/d/msgid/django-users/5f9399d4-6a48-450a-b2bd-2e33a296b1b5%40googlegroups.com?utm_medium=email&utm_source=footer>
>>> .
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>> --
>> You received this message because you are subscribed to a topic in the
>> Google Groups "Django users" group.
>> To unsubscribe from this topic, visit https://groups.google.com/d/to
>> pic/django-users/GDuwEZXyQ3k/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to
>> django-users+unsubscr...@googlegroups.com.
>> To post to this group, send email to django-users@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/ms
>> gid/django-users/CAB%2BAj0vGREMexQPAg8tpAaMP_3iL8QNPCk9WbQFZ
>> SZ9hrsksHw%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-users/CAB%2BAj0vGREMexQPAg8tpAaMP_3iL8QNPCk9WbQFZSZ9hrsksHw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> 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+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@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/CAKd6oBzJcSgdBfD3AJ0oY-Sg1pQjYF3yzt5kV5QB%
> 3DZzxMC0iTA%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAKd6oBzJcSgdBfD3AJ0oY-Sg1pQjYF3yzt5kV5QB%3DZzxMC0iTA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/CAB%2BAj0tkRkackPjOX09hdGJrB2Jusc8SQwyDZAB7ApPdnC3Ztw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to