Thursday, July 30, 2015

Re: Does imports of related models affect cascade deletion?

I will fetch the actual piece of code and build a small test case out of it, will reply again in this thread when I have some more info.

On Thu, Jul 30, 2015 at 6:39 PM, Marc Aymerich <glicerinu@gmail.com> wrote:
On Thu, Jul 30, 2015 at 3:38 PM, Markus Amalthea Magnuson
<markus.magnuson@gmail.com> wrote:
> Hey,
>
> I stumbled upon a piece of code and a comment that says this:
>
> Deleting a model object that has related objects will only cascade delete
> those objects if their models have been imported.
>
> Is this true? I have not found it in the documentation and would like to add
> a reference to the code comment so others won't be as confused as I am.

Is this models module called models.py and does it live inside a
Django application listed in INSTALLED_APPS?


--
Marc

--
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/topic/django-users/wLiEuzqUVeQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BDCN_tKE56qJPJ339uL0Zo%2BvEDSsPdTnLMODHC_3H4hd856dw%40mail.gmail.com.
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+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAOXH8SXRVShSrLuPjZuPDPhbEEdc-Bi9v5JQm-xHf181iCzMKA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re. My Django Book.

 Say I was wondering if anyone else has ever read this particulkar Django book called "Sams Teach Yourself Django in 24 Hours" I checked out of the library yesterday? [yeah I know it's one of the "24 hours" series] Does it really teach the beginner a lot of things concerning Django?


   

--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/de78f7fa-cb87-4d88-a9c6-886f43841bb2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Custom Field

I wanted your help to make a custom Field Type'm studying it at the time and I'm not getting. I'll put it to you here the code I've done so far. I hope your help.

--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/10405523-935b-447d-9dea-70e4c6d68de4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Two-to-Many Mapping between Models in Django ORM

Hi Tom,

That was approximately what I was planning to do, I shall do some experimenting to see if I can do any more.  I was just wondering if there was a clever feature for this sort of thing, as ORMs seem pretty magic already!

Thanks,
Rich

On Thursday, 30 July 2015 17:15:40 UTC+1, Tom Evans wrote:
On Thu, Jul 30, 2015 at 4:08 PM, Rich Lewis <rich....@gmx.co.uk> wrote:
> Dear All,
>
> I'm new to the Django ORM, and quite new to ORMs in general.  I have two
> models (lets call them A and B) between which I have an interesting mapping.
> There are precisely 2 B instances associated with each A instance.  Each A
> instance can have many B instances.  The order of Bs are important for As.
>
> I want to do something like this:
>
> class A(models.Model):
>   b_1 = models.ForeignKey(B)
>   b_2 = models.ForeignKey(B)

This would fail here, when you have more than one foreign key to the
same model, you must supply a related_name argument on one of them,
which is used for the inverse relation on the related object. Which
leads us in to...

>
> class B(models.Model):
>   pass
>
> Such that i can do:
>
>>>> b1, b2, b3 = B(), B(), B()
>>>> a1, a2 = A(b_1=b1, b_2=b2), A(b_1=b2, b_2=b3)
>>>> b2.as
> [<A ... >, <A ... >] #(order doesn't matter)

Typically, B instances would have an attribute named 'a_set' (the
lower case model name of the model it is related to, with "_set"
appended). When you have multiple relationships with the same model,
as I mentioned you must supply your own related_name arguments. In
which case, with the models you mentioned, you could then do something
like this:

>>> b2.a1_set.all() | b2.a2_set.all()
> [<A ..>, <A ..>]

>
> I expect I could eventually do something a bit hacky that would work, but
> what would be the best way to handle this?

I would say the above is the hacky way. You could have the same B
assigned to b_1 and b_2 and would have duplicates in the set, and no
way to specify DB constraints to limit it.

Given that you anticipate being able to retrieve all the A's for a
particular B, regardless of how they are related, I would model it as
a M2M with a through table holding any additional information about
the relationship, using the through table to add DB level constraints.

Cheers

Tom

--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/daa601f5-12d5-44fe-8c58-f2f052629261%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Two-to-Many Mapping between Models in Django ORM

Oops sorry I meant every B instance can have multiple A instances.  Sorry!

On Thursday, 30 July 2015 16:21:37 UTC+1, monoBOT monoBOT wrote:

2015-07-30 16:08 GMT+01:00 Rich Lewis <rich....@gmx.co.uk>:
There are precisely 2 B instances associated with each A instance.  Each A instance can have many B instances

Isnt that a contradiction?



--
monoBOT
Visite mi sitio(Visit my site): monobotsoft.es/blog/

--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/40eadb77-0205-403d-92a8-52859645c4fc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Re: Does imports of related models affect cascade deletion?

On Thu, Jul 30, 2015 at 3:38 PM, Markus Amalthea Magnuson
<markus.magnuson@gmail.com> wrote:
> Hey,
>
> I stumbled upon a piece of code and a comment that says this:
>
> Deleting a model object that has related objects will only cascade delete
> those objects if their models have been imported.
>
> Is this true? I have not found it in the documentation and would like to add
> a reference to the code comment so others won't be as confused as I am.

Is this models module called models.py and does it live inside a
Django application listed in INSTALLED_APPS?


--
Marc

--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BDCN_tKE56qJPJ339uL0Zo%2BvEDSsPdTnLMODHC_3H4hd856dw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Re: Two-to-Many Mapping between Models in Django ORM

On Thu, Jul 30, 2015 at 4:08 PM, Rich Lewis <rich.lewis@gmx.co.uk> wrote:
> Dear All,
>
> I'm new to the Django ORM, and quite new to ORMs in general. I have two
> models (lets call them A and B) between which I have an interesting mapping.
> There are precisely 2 B instances associated with each A instance. Each A
> instance can have many B instances. The order of Bs are important for As.
>
> I want to do something like this:
>
> class A(models.Model):
> b_1 = models.ForeignKey(B)
> b_2 = models.ForeignKey(B)

This would fail here, when you have more than one foreign key to the
same model, you must supply a related_name argument on one of them,
which is used for the inverse relation on the related object. Which
leads us in to...

>
> class B(models.Model):
> pass
>
> Such that i can do:
>
>>>> b1, b2, b3 = B(), B(), B()
>>>> a1, a2 = A(b_1=b1, b_2=b2), A(b_1=b2, b_2=b3)
>>>> b2.as
> [<A ... >, <A ... >] #(order doesn't matter)

Typically, B instances would have an attribute named 'a_set' (the
lower case model name of the model it is related to, with "_set"
appended). When you have multiple relationships with the same model,
as I mentioned you must supply your own related_name arguments. In
which case, with the models you mentioned, you could then do something
like this:

>>> b2.a1_set.all() | b2.a2_set.all()
> [<A ..>, <A ..>]

>
> I expect I could eventually do something a bit hacky that would work, but
> what would be the best way to handle this?

I would say the above is the hacky way. You could have the same B
assigned to b_1 and b_2 and would have duplicates in the set, and no
way to specify DB constraints to limit it.

Given that you anticipate being able to retrieve all the A's for a
particular B, regardless of how they are related, I would model it as
a M2M with a through table holding any additional information about
the relationship, using the through table to add DB level constraints.

Cheers

Tom

--
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-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1%2B7-KieRLGT_hG-UDC6zeyg%2Bb8d%3D7XtEK4EEM8nFvzFng%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.