Re: Symmetrical, Self-referencing ManyToManyField with Through Table

2020-06-25 Thread Dan Madere
I tried out the example code, and can replicate this. What's interesting is 
that if I try removing the ContactConnection model, and the "through" 
attribute, this allows Django to create the intermediate table on its own, 
and then your get_connections() method works as expected! It seems like 
using a custom through table is causing this somehow. I haven't found a fix 
but will keep looking.

On Thursday, June 25, 2020 at 8:52:47 AM UTC-4 jes...@gmail.com wrote:

> Mike, Thanks for your suggestions. 
>
> Just a stab in the dark - have you tried giving from_contact a 
>> related_name? 
>>
>
> Yes, I have tried a few different combinations of providing a single 
> related_name and various naming conventions when providing related_name on 
> both ForeignKey fields without success.
>  
>
>> Another stab ... maybe you could just display the 
>> ContactConnectionAdmin(admin.ModelAdmin) with model=ContactConnection? 
>>
>>
> Yes, adding a ModelAdmin for the through table will show all of the 
> relationships, but it still requires searching both ForeignKey fields to 
> capture all of the relationships, regardless of which Contact hey were 
> created on.
>
> Workarounds are to add two Inlines, one for each through table foreign 
> key, or perform a compound query to combine the two results. I just figured 
> that since the capability was added in Django 3.0, that the symmetric 
> queries were included as well. My guess is that it is possible with the 
> correct configuration / naming conventions. I'll dive into the code to see 
> if anything pops up.
>
> Thanks!
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/7ee482c1-60ea-482f-8ffb-5f71ff92a1edn%40googlegroups.com.


Re: Symmetrical, Self-referencing ManyToManyField with Through Table

2020-06-25 Thread Dan Madere
I don't know of a way to configure the admin do that, but one solution 
would be to use signals to notice when one-way records are created, then 
automatically create the record for reverse relationship. We need to notice 
when records are deleted as well. This approach would allow showing one 
inline that shows the combined results. I was able to get it working with 
this: https://gist.github.com/dgmdan/e7888a73c14446dccd7ad9aaf5055b10
Hope this helps!

On Thursday, June 25, 2020 at 1:53:45 PM UTC-4 jes...@gmail.com wrote:

> After reviewing the tests, I think I now understand what is going on.
>
> Basically, for symmetric ManyToManyField, Django creates entries for both 
> directions automatically if the correct interface is used. From the test 
> models:
>
> class PersonSelfRefM2M(models.Model):
> name = models.CharField(max_length=5)
> sym_friends = models.ManyToManyField('self', 
> through='SymmetricalFriendship', symmetrical=True)
>
>
> class SymmetricalFriendship(models.Model):
> first = models.ForeignKey(PersonSelfRefM2M, models.CASCADE)
> second = models.ForeignKey(PersonSelfRefM2M, models.CASCADE, 
> related_name='+')
> date_friended = models.DateField()
>
>
> And the tests:
>
> def test_self_referential_symmetrical(self):
> tony = PersonSelfRefM2M.objects.create(name='Tony')
> chris = PersonSelfRefM2M.objects.create(name='Chris')
> SymmetricalFriendship.objects.create(
> first=tony, second=chris, date_friended=date.today(),
> )
> self.assertSequenceEqual(tony.sym_friends.all(), [chris])
> # Manually created symmetrical m2m relation doesn't add mirror entry
> # automatically.
> self.assertSequenceEqual(chris.sym_friends.all(), [])
> SymmetricalFriendship.objects.create(
> first=chris, second=tony, date_friended=date.today()
> )
> self.assertSequenceEqual(chris.sym_friends.all(), [tony])
>
> def test_add_on_symmetrical_m2m_with_intermediate_model(self):
> tony = PersonSelfRefM2M.objects.create(name='Tony')
> chris = PersonSelfRefM2M.objects.create(name='Chris')
> date_friended = date(2017, 1, 3)
> tony.sym_friends.add(chris, through_defaults={'date_friended': 
> date_friended})
> self.assertSequenceEqual(tony.sym_friends.all(), [chris])
> self.assertSequenceEqual(chris.sym_friends.all(), [tony])
> friendship = tony.symmetricalfriendship_set.get()
> self.assertEqual(friendship.date_friended, date_friended)
>
>
> So the tests show that the add() method needs to be used to create both 
> sides of the relationship. I assume that the Admin page does not use the 
> add method, but creates the intermediate entries which would require both 
> the be added for each entry.
>
> Is it possible to configure the Admin models to use add() or to create 
> both directions automatically?
>
> Thanks!
>
>
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b8e0927d-6079-4a0a-91db-044d6f393998n%40googlegroups.com.


Re: Django queryset filtering

2020-06-26 Thread Dan Madere
I'd make two queries. One to see which businesses currently have active 
licenses, then a second to get the expired licenses, excluding the 
businesses from the first query. Here's some example code, assuming the 
LicenseIssue model has a "business" foreign key field:

*active_business_ids = 
LicenseIssue.objects.filter(expiry_date__gt=TODAY).values_list('business_id', 
flat=True).distinct()*
*expired_licenses = 
LicenseIssue.objects.filter(expiry_date__lte=TODAY).exclude(business_id__in=active_business_ids).order_by('-expiry_date')*

On Wednesday, June 24, 2020 at 6:25:18 PM UTC-4 mtp...@gmail.com wrote:

> I have a queryset that returns all the expired licenses objects as shown 
> below.
>
>
> *qs = 
> LicenseIssue.objects.filter(expiry_date__lte=TODAY).order_by('-expiry_date')*
> 
>
> Here is my situation:
> There exists multiple issued licenses of different businesses for year 
> 2020 and their  previous issued licenses for the for year 2019, 2018 and so 
> on. The above queryset results to listing all expired licenses though I'd 
> like to make it possible if there exists issued licenses for year 2020 
> their previous issued licenses should not appear in the expired licenses 
> queryset. Please advise on this.
>
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9a0c6125-efc6-4127-98cc-30ffadd01195n%40googlegroups.com.


Re: Django ContentTypes required?

2020-07-06 Thread Dan Madere
I agree that it should work. Django uses ContentType in the admin and user 
authentication, but you mention that you have removed these from 
INSTALLED_APPS, so I don't get it. I'd try clearing .PYC files first. Then 
maybe a sanity check.. double check what settings file you're actually 
using, and if INSTALLED_APPS contains what you expect.

Dan

On Monday, July 6, 2020 at 2:33:19 PM UTC-4 jzt...@gmail.com wrote:

> Hello,
>
> I am not using any of the ContentType relations, etc right now in my 
> Django 2.12 Python 3.6 application, am I able to run the application 
> without having django.contrib.contenttypes in the INSTALLED_APPS? Is there 
> a piece I am not understanding that Django uses it for in the background?
>
> I thought I had it working with it removed but I am getting:
>
> Model class django.contrib.contenttypes.models.ContentType doesn't declare 
> an explicit app_label and isn't in an application in INSTALLED_APPS.
>
> I have nothing in the INSTALLED_APPS except my apps and:
>
> 'django.contrib.staticfiles',
> 'rest_framework',
>
> Was trying an attempt where I avoided migrating contenttypes into a legacy 
> database.
>
> Best,
>
> JJ
>

-- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cbce5c34-652a-42ac-84b5-0c881abce893n%40googlegroups.com.


Re: POSTGRES CONNTECTION TIMEOUT WHEN USING AN API

2020-07-06 Thread Dan Madere
Sounds like nginx is timing out waiting for Django, due to the slow query. 
You can set proxy_connect_timeout and proxy_read_timeout in your nginx 
config to allow it to wait longer. Another approach would be to optimize 
the SQL query and add indexes, to have the query run faster and not cause 
the timeout.

Dan

On Monday, July 6, 2020 at 9:24:48 AM UTC-4 sunne...@gmail.com wrote:

> I get this error from my postgresql log also : could not send data to 
> client: Broken pipe
> *AJAYI Sunday *
> (+234) 806 771 5394 <+234%20806%20771%205394>
> *sunne...@gmail.com*
>
>
>
> On Mon, Jul 6, 2020 at 2:06 PM Sunday Iyanu Ajayi  
> wrote:
>
>> I have a database sql function that I am running fine when on the 
>> database but once i link the output via an api I created  and test via 
>> postman, I get Nginx 502 error and on my postgresql log I get : Çonnection 
>> Timeout error.
>>
>> The sql db function link to a foreign table and pulls some data (between 
>> 500-2000 rows)
>>
>> Please how can I fix this?
>>
>>
>> *AJAYI Sunday *
>> (+234) 806 771 5394 <+234%20806%20771%205394>
>> *sunne...@gmail.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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/9a815404-3c51-4a1f-b85d-ba6886dfc9a0n%40googlegroups.com.