Re: Form.has_changed() == False and Form.changed_data empty

2021-04-11 Thread Lars Liedtke
Hello Gabriel,

thank you for your reply, I use the widgets own rendering in my template.

{{ form.customer }}

where "form" is the Form class and customer the checkbox-widget passed
via the context.

The Problem is not a rendering Problem (I think) because the checkbox
works as intended. Only the information that the checkbox has been
changed from checked to unchecked with a new post request, is not
recognized inside the post() method of my view. The values in
Form.cleaned_data represent the correct values (customer is False, where
it was True in the request before). But due to the fact that I am
checking for Form.is_changed() my code does not detect the change. Also
Form.changed_data does not contain "customer". THis is my code in
post(), I've added some comments:

    def post(self, request, *args, **kwargs):
    company: models.Company = get_object_or_404(models.Company,
pk=kwargs['pk'])
    self.initialize_forms(request, company) # initializes the Form
and its Formsets
    if not_valid := self._check_form_validity(request, *args, **kwargs):
    return not_valid

    if self.company_form.has_changed(): # This is False!
    if 'agency' in self.company_form.changed_data:
    self.__update_company_type(company, models.Agency)
    if 'customer' in self.company_form.changed_data: #
company_form does not contain "customer"
    self.__update_company_type(company, models.Customer)
    company = self.company_form.save()
    if self.address_formset.has_changed():
    self.address_formset.save()
    if self.ssh_key_formset.has_changed():
    self.ssh_key_formset.save()
    if self.email_formset.has_changed():
    self.email_formset.save()

The raw Post-Request is correct as well, I checked in my browser and in
my tests. This is how I try to test the unchecking:

class UpdateCompanyViewsTests(TestCase):
    POST_REQUEST_DATA = dict({'name': "Apu's Kwik'E'Mart",
  'addresses-TOTAL_FORMS': '1',
  'addresses-INITIAL_FORMS': '0',
  'addresses-MIN_NUM_FORMS': '0',
  'addresses-MAX_NUM_FORMS': '1000',
  'addresses-0-street': '',
  'addresses-0-number': '',
  'addresses-0-postcode': '',
  'addresses-0-place': '',
  'addresses-0-country_code': '',
  'ssh_keys-TOTAL_FORMS': '1',
  'ssh_keys-INITIAL_FORMS': '0',
  'ssh_keys-MIN_NUM_FORMS': '0',
  'ssh_keys-MAX_NUM_FORMS': '1000',
  'ssh_keys-0-ssh_key': '',
  'email_addresses-TOTAL_FORMS': '1',
  'email_addresses-INITIAL_FORMS': '0',
  'email_addresses-MIN_NUM_FORMS': '0',
  'email_addresses-MAX_NUM_FORMS': '1000',
  'email_addresses-0-email_address': ''
  })

[...]

def test_post_company_edit_type_remove_customer(self):
    company = Company.objects.create(id='1', name="Apu's Kwik'E'Mart")
    get_response = self.client.get(f'/company/1/')
    post_set_customer_data = {**self.POST_REQUEST_DATA,
  'csrfmiddlewaretoken':
get_response.cookies['csrftoken'].value,
  'agency': '',
  'customer': 'on',
  }
    post_set_customer_response = self.client.post(f'/company/1/',
data=post_set_customer_data, follow=True)
    self.assertEqual(post_set_customer_response.status_code, 200)
    post_remove_customer_data = {**self.POST_REQUEST_DATA,
 'csrfmiddlewaretoken':
post_set_customer_response.cookies['csrftoken'].value,
 'agency': '',
 'customer': '',
 }
    post_remove_customer_response = self.client.post(f'/company/1/',
data=post_remove_customer_data, follow=True)
    self.assertEqual(post_remove_customer_response.status_code, 200)
    self.assertFalse(hasattr(company, 'customer'))

and this test fails because company still has got the customer attribute.

Am 12.04.21 um 16:12 schrieb Gabriel Araya Garcia:

> I have checkboxes in my css grid and there is name, type=chekbox",
> etc. In may view django I have captured the value of the
> check template with             valor_xx = request.POST.get(str(xx))  
> # valor del check directamente desde template
> in POST method.
> But, all of this in my view based in function.
> Last night finished with this and I'd checked this running perfectly.
>  You have take in mind that in template 

Re: Do I really need Groups for permission management?

2021-04-11 Thread Hedrick Godson's
If your project involves user management where by some admins can create
custom users and grant them permissions I think it is better for you to use
Groups where by admins create a group e.g Librarian and then grant it
custom permissions and after that when admins create a new user he/she can
just choose a group where he can assign that user.
And in the case of permissions you can use custom decorators on ur views

On Mon, 12 Apr 2021, 02:31 Mike Dewhirst,  wrote:

> If you are using custom user, you can write methods which return booleans
> and which can be seen by the template.
>
> I wrote aasmall utility method is_member(groupname) which returns the
> boolean.
>
> I make liberal use of groups which work very well for me. In my custom
> user I can easily add a method to test whether that user is a member of
> that group.
>
> On the question in your subject line about permissions, I refuse to
> contemplate individual permissions. I will ALWAYS use group permissions and
> name my groups to represent roles.  Much more understandable by users (and
> me) and much much much more manageable than individual permissions.
>
> Ymmv
>
> Mike
>
>
> --
> (Unsigned mail from my phone)
>
>
>
>  Original message 
> From: Manuel Buri 
> Date: 12/4/21 08:48 (GMT+10:00)
> To: Django users 
> Subject: Do I really need Groups for permission management?
>
> Hi,
>
> I would love to know more about the advantages / disadvantages of using
> groups for permission management.
>
> Currently my project uses Booleans for is_workspace_owner (True or False)
> and consequently does not allow to show certain links via template
> if-checks as well as some user_passes_test. So basically I am doing the
> permission management link this which works pretty well. I am asking myself
> if I should change to groups or just leave it like this?
>
> What do you think?
>
> Thanks a lot for your inputs/opinions/tipps.
> Manuel
>
> --
> 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/4ce20b42-df22-4f0d-b8c5-7d8ff5476bb0n%40googlegroups.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/607386e1.1c69fb81.d21fc.ed34SMTPIN_ADDED_MISSING%40gmr-mx.google.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/CAJAxQDm4im_TLEapRFw4reKHOMDiOgZ92fc6ui95xUm23wT0Cg%40mail.gmail.com.


RE: Do I really need Groups for permission management?

2021-04-11 Thread Mike Dewhirst
If you are using custom user, you can write methods which return booleans and 
which can be seen by the template. I wrote aasmall utility method 
is_member(groupname) which returns the boolean. I make liberal use of groups 
which work very well for me. In my custom user I can easily add a method to 
test whether that user is a member of that group.On the question in your 
subject line about permissions, I refuse to contemplate individual permissions. 
I will ALWAYS use group permissions and name my groups to represent roles.  
Much more understandable by users (and me) and much much much more manageable 
than individual permissions.YmmvMike--(Unsigned mail from my phone)
 Original message From: Manuel Buri  
Date: 12/4/21  08:48  (GMT+10:00) To: Django users 
 Subject: Do I really need Groups for permission 
management? Hi,I would love to know more about the advantages / disadvantages 
of using groups for permission management.Currently my project uses Booleans 
for is_workspace_owner (True or False) and consequently does not allow to show 
certain links via template if-checks as well as some user_passes_test. So 
basically I am doing the permission management link this which works pretty 
well. I am asking myself if I should change to groups or just leave it like 
this?What do you think?Thanks a lot for your inputs/opinions/tipps.Manuel



-- 
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/4ce20b42-df22-4f0d-b8c5-7d8ff5476bb0n%40googlegroups.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/607386e1.1c69fb81.d21fc.ed34SMTPIN_ADDED_MISSING%40gmr-mx.google.com.


Do I really need Groups for permission management?

2021-04-11 Thread Manuel Buri
Hi,

I would love to know more about the advantages / disadvantages of using 
groups for permission management.

Currently my project uses Booleans for is_workspace_owner (True or False) 
and consequently does not allow to show certain links via template 
if-checks as well as some user_passes_test. So basically I am doing the 
permission management link this which works pretty well. I am asking myself 
if I should change to groups or just leave it like this?

What do you think?

Thanks a lot for your inputs/opinions/tipps.
Manuel

-- 
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/4ce20b42-df22-4f0d-b8c5-7d8ff5476bb0n%40googlegroups.com.


how make investment website with django

2021-04-11 Thread shimaa

Hi I want help creating an investment site with django; I want to know how 
to make deposits and withdrawals for each client, to subscribe to a 
specific package for a specific number, to bring in the daily profits for 
each package,and  add the bonus to the client, and how to use Skrill with 
Django Please help me

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/172759a6-ceed-4ee9-b6bc-43c3253ad874n%40googlegroups.com.


Re: QuerySet performance on large datasets

2021-04-11 Thread Peter of the Norse
Follow up to my own message.  I found a way of testing this that I think 
everyone should look at:

$ ./manage.py shell
Python 3.8.5 (default, Jul 21 2020, 10:48:26)
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import logging
>>> l = logging.getLogger('django.db.backends')
>>> l.setLevel(logging.DEBUG)
>>> l.addHandler(logging.StreamHandler())

This will allow you to see what SQL is actually being run.  That way you can do 
things like comparing which statement is faster when you do things side by side.

>>> from django.contrib.auth.models import User
>>> qs = User.objects.all()
>>> val = qs.earliest('date_joined') if qs.exists() else None
(0.000) SELECT (1) AS "a" FROM "auth_user" LIMIT 1; args=()
(0.026) SELECT "auth_user"."id", "auth_user"."password", 
"auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", 
"auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM 
"auth_user" ORDER BY "auth_user"."date_joined" ASC LIMIT 1; args=()
>>> qs = User.objects.all()  # Don’t use the cache
>>> val = qs.earliest('date_joined') if bool(qs) else None
(0.024) SELECT "auth_user"."id", "auth_user"."password", 
"auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", 
"auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM 
"auth_user"; args=()
(0.000) SELECT "auth_user"."id", "auth_user"."password", 
"auth_user"."last_login", "auth_user"."is_superuser", "auth_user"."username", 
"auth_user"."first_name", "auth_user"."last_name", "auth_user"."email", 
"auth_user"."is_staff", "auth_user"."is_active", "auth_user"."date_joined" FROM 
"auth_user" ORDER BY "auth_user"."date_joined" ASC LIMIT 1; args=()

This is a new install with only one user.  Your numbers are likely to be much 
higher and have significant differences.


> On Feb 12, 2021, at 5:50 PM, Peter of the Norse  wrote:
> 
> bool(qs) is recommended when the queryset has already been run.  If you do 
> something like 
> 
> val = qs.earliest(some_property) if bool(qs) else None
> 
> then there isn’t a performance gain because it still has to run qs.exists() 
> to even see if there is a value.  Later things like that will be cached 
> though.
> 
> Although I might be wrong about this.  I have not dug enough to be certain.
> 
>> On Jan 12, 2021, at 4:10 AM, 'Juergen H' via Django users 
>> mailto:django-users@googlegroups.com>> wrote:
>> 
>> Dear django users,
>> 
>> when working on very large datasets (millions of items) and performing 
>> queries on it, I have a few questions regaring the performance.
>> 
>> From my understanding using `bool(qs)` is preferred over using `qs.exists()` 
>> when the queryset is used later on, as `bool(qs)` already evaluates and 
>> caches the qs, which `qs.exists()` does not. Is that correct?
>> 
>> Is the above assumption also true, when the qs that has bee evaluated by 
>> using `bool(qs)` afterwards is accessed by e.g. 
>> `qs.earliest(some_property)`,? 
>> Or does `qs.earliest(some_property)` generate a new qs and hence there is 
>> not really an advantage of having the original qs already evaluated by 
>> `bool(qs)`?
>> 
>> E.g. using `.filter()` will surely create and return a new qs, as stated in 
>> the docu and in this case I am pretty sure, the advantage of evaluating and 
>> caching the qs by using `bool(qs)` is lost here.
>> But for `earliest()` or `latest()` the docu only states, that an object is 
>> returned. It does not say anything about a new queryset being created.
>> Hence my questions..
>> 
>> Thanks a lot and happy coding
>> Juergen
>> 
>> -- 
>> 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/571d5cb6-bd53-4db4-aad1-2a820d71782en%40googlegroups.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/23A6B39A-97B8-4CF7-9CAF-B45BD6B09B35%40gmail.com.


Re: How to properly document and test models with natural keys

2021-04-11 Thread Anusheel Bhushan
HI Olivier - 
Django typically has separate settings files for each environment. You can 
also use different environment variables to use when starting the app.
RE: models, here's an example of serialization that could be helpful to 
show how to serialize/deserialize models with foreign keys 
: 
https://github.com/imagineai/create-django-app/blob/master/todoapp/serializers.py

re: unit tests, see some examples of serializers 
here: https://github.com/imagineai/create-django-app/tree/master/todoapp/tests

Both the serializers and the tests above are generated (from a yaml type 
config).

Happy to answer more specific qs...
On Monday, March 29, 2021 at 2:59:12 AM UTC-7 Olivier wrote:

> Hello,
>
> I'm thinking about adding natural keys to a lot of existing models (Djano 
> 3.1.7) to help initializing dev or prod database.
>
> 1. How best to unit test  models using natural keys ?
> I was thinking of separately testing  both serialization and 
> deserialization but would be very curious to learn about examples or 
> alternatives, specifically when Foerign key relations existe between models.
>
> 2. For any unit test, you may have to hand write a YAML sample.
> How is it best to write this sample ?
>
> 3. Suggestions. Recommendations. Pointers ?
>
> Best regards
>

-- 
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/9913bb40-4081-47a4-b668-d7c14c00b328n%40googlegroups.com.


Re: Form.has_changed() == False and Form.changed_data empty

2021-04-11 Thread Gabriel Araya Garcia
I have checkboxes in my css grid and there is name, type=chekbox", etc. In
may view django I have captured the value of the check template with
  valor_xx = request.POST.get(str(xx))   # valor del check directamente
desde template
in POST method.
But, all of this in my view based in function.
Last night finished with this and I'd checked this running perfectly.
 You have take in mind that in template you must ask if the value check
field is chequed or not with {% if.. %}..{%else%}{%endif%}. This is sent
from context.
If you need some help,
Regards

Gabriel Araya Garcia
GMI - Desarrollo de Sistemas Informáticos
Santiago de Chile



El dom, 11 abr 2021 a las 10:49, Lars Liedtke () escribió:

> Hello,
>
> I have got two checkboxes in a Form on Django 3.1, with which everything
> works fine, but unchecking them.
>
> I use a class derived from UpdateView and in its post() method I check
> for if my form has changed. This works fine for an initial empty form
> and checking form.has_changed(); after then handling the change and
> calling get() with the objects id in the Browser. I get the Form back
> with checked checkboxes. The values in the database are fine as well.
>
> So now I want to test with unchecking those checkboxes and it does not
> work. Form.has_changed() == False and Form.changed_data is empty. While
> the correct values are in Form.cleaned_data(). But my code does not
> detect these values because I check for Form.has_changed().
>
> Am I understanding something wrong here or do I have to check if there
> is a difference from the values I get in Form.cleaned_data to what my
> current state in the database is myself?
>
> I am happy to show the code. But I thought I'd ask about it in general
> before posting lots of code.
>
> ---
> punkt.de GmbH
> Lars Liedtke
> .infrastructure
>
> Kaiserallee 13a
> 76133 Karlsruhe
>
> Tel. +49 721 9109 500
> https://infrastructure.punkt.de
> i...@punkt.de
>
> AG Mannheim 108285
> Geschäftsführer: Jürgen Egeling, Daniel Lienert, Fabian Stein
>
> --
> 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/13e0719e-8d06-e37c-b07e-1c6deb1f84e6%40punkt.de
> .
>

-- 
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/CAKVvSDDROtUwj_HR1B5OdbjASD2udkgbyy24MXKOCxzz%3DmhyCg%40mail.gmail.com.


Form.has_changed() == False and Form.changed_data empty

2021-04-11 Thread Lars Liedtke
Hello,

I have got two checkboxes in a Form on Django 3.1, with which everything
works fine, but unchecking them.

I use a class derived from UpdateView and in its post() method I check
for if my form has changed. This works fine for an initial empty form
and checking form.has_changed(); after then handling the change and
calling get() with the objects id in the Browser. I get the Form back
with checked checkboxes. The values in the database are fine as well.

So now I want to test with unchecking those checkboxes and it does not
work. Form.has_changed() == False and Form.changed_data is empty. While
the correct values are in Form.cleaned_data(). But my code does not
detect these values because I check for Form.has_changed().

Am I understanding something wrong here or do I have to check if there
is a difference from the values I get in Form.cleaned_data to what my
current state in the database is myself?

I am happy to show the code. But I thought I'd ask about it in general
before posting lots of code.

---
punkt.de GmbH
Lars Liedtke
.infrastructure

Kaiserallee 13a 
76133 Karlsruhe

Tel. +49 721 9109 500
https://infrastructure.punkt.de
i...@punkt.de

AG Mannheim 108285
Geschäftsführer: Jürgen Egeling, Daniel Lienert, Fabian Stein

-- 
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/13e0719e-8d06-e37c-b07e-1c6deb1f84e6%40punkt.de.