UpdateView: input always rejected

2018-11-20 Thread clavierplayer
I need to render some form fields by hand in order to meet style 
requirements. I am using UpdateView instead of a ModelForm since I just 
need a couple of foreign key fields that are already defined on the model, 
and I am using html  elements to prevent invalid data from being 
entered. 

I'm running into trouble because when I choose an option and click the 
submit button, Django rejects the input and raises a validation error 
because the field is required. I know the data should be valid because it 
all came straight from the database, but Django appears to be throwing it 
away. I can't find UpdateView's clean() methods to try to catch the error 
that way. I've tried overriding post() just to see where the problem is 
occurring, but the option I chose seemed to just disappear; I don't know 
what Django is doing with it or why it thinks its invalid, so I don't know 
where to try to catch and fix the problem.

I have two separate html forms with two submit buttons; I separated them 
because they seemed easier to style that way, and also because users will 
generally need to change one or the other, not both; but two forms might be 
the wrong way to go about it. But that doesn't explain why data that came 
straight from the database is invalid. They both fail with the same 
ValidationErrors (i.e. fields are required). 

Thank you for any ideas or insight!
Heather


class Order(models.Model):

order_id = models.IntegerField(primary_key=True)


# (Some additional model fields.)

service_level = models.ForeignKey('manifest.ServiceLevel', 
on_delete=models.PROTECT)
priority = models.ForeignKey('fulfillment.Priority', 
on_delete=models.PROTECT)



class Priority(models.Model):
"""fulfillment priority levels"""
TYPES = Choices(
('FR', 'FLASH_RUSH', 'Flash Rush'),
('RU', 'RUSH', 'Rush'),
('RG', 'REGULAR', 'Regular'),
)

code = models.CharField(max_length=2, choices=TYPES)
name = models.CharField(max_length=50)



class ServiceLevel(models.Model):
"""levels of service which a carrier provides"""

code = models.CharField(max_length=10, primary_key=True)
name = models.CharField(max_length=50)

# (Some additional model fields.)



class WMSOrderUpdateView(UpdateView):
context_object_name = 'order'
template_name = 'fulfillment/order_detail.html'
fields = ['priority', 'service_level']

def get_context_data(self, **kwargs):
context = super().get_context_data()
context['priorities'] = Priority.objects.all()
context['ship_methods'] = ServiceLevel.objects.all()
return context






{% csrf_token %}

{% if order.priority.code == 'FR' %}

{% elif order.priority.code == 'RU' %}

{% else %}

Priority:
{% endif %}



{% for priority in priorities %}
{{ priority.name }}
{% endfor %}







{% csrf_token %}
Shipping Method: 

{% for method in ship_methods %}
{{ method.name|title }}
{% endfor %}








-- 
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/26b1dcbd-54c7-431c-bd0e-5740ba470aa3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: avoiding import-time queries

2018-07-27 Thread clavierplayer
I've been experimenting with it and it seems to be working pretty well. 
Thank you, Julio!

On Thursday, July 26, 2018 at 2:28:18 PM UTC-4, Julio Biason wrote:
>
> Hi Clarvierplayer,
>
> Dunno if that's a best practice, but I'd add a module in the same app with 
> functions to retrieve the information and use cache (
> https://docs.djangoproject.com/en/2.0/topics/cache/#basic-usage) 
> copiously. So, instead of loading the values at start up time, the values 
> would be filled only after the first time it was requested.
>
> On Thu, Jul 26, 2018 at 3:21 PM, > 
> wrote:
>
>> I'm looking for some best-practice (or at least workable) suggestions for 
>> avoiding import-time queries. I have some database records that are 
>> constant, and these constants are used all over the application. Is there 
>> any way to get those constants out of the database once at startup? 
>>
>> At present, I am working on a set of apps with a single underlying 
>> database. And they wouldn't build at all when I tried to migrate a new test 
>> database because of import-time queries, because it wants to validate the 
>> references to those constants at build time (and can't, because in this 
>> case my new test database has no tables yet). Is there any way to store 
>> these model records in something that will persist between builds (kind of 
>> like test fixtures)? A StackOverflow discussion 
>> 
>>  suggested 
>> the Python Lazy Object Proxy, but that project appears to have been 
>> abandoned. The Django docs themselves simply recommend avoiding 
>> query-at-import behaviors--I can definitely see it causes all kinds of 
>> problems that are not easy to circumvent--but they make no recommendations 
>> for alternatives. Is it indeed necessary to keep querying for these all 
>> over the app? Is there a third-party solution that I've overlooked?
>>
>> Django 2.0. Thank you!
>>
>> -- 
>> 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/9510552d-9afe-47b0-842e-6f80e9a28bbe%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> *Julio Biason*, Sofware Engineer
> *AZION*  |  Deliver. Accelerate. Protect.
> Office: +55 51 3083 8101  |  Mobile: +55 51 *99907 0554*
>

-- 
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/b487bf9d-359c-4541-82a9-2b6228fe9437%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


avoiding import-time queries

2018-07-26 Thread clavierplayer
I'm looking for some best-practice (or at least workable) suggestions for 
avoiding import-time queries. I have some database records that are 
constant, and these constants are used all over the application. Is there 
any way to get those constants out of the database once at startup? 

At present, I am working on a set of apps with a single underlying 
database. And they wouldn't build at all when I tried to migrate a new test 
database because of import-time queries, because it wants to validate the 
references to those constants at build time (and can't, because in this 
case my new test database has no tables yet). Is there any way to store 
these model records in something that will persist between builds (kind of 
like test fixtures)? A StackOverflow discussion 

 suggested 
the Python Lazy Object Proxy, but that project appears to have been 
abandoned. The Django docs themselves simply recommend avoiding 
query-at-import behaviors--I can definitely see it causes all kinds of 
problems that are not easy to circumvent--but they make no recommendations 
for alternatives. Is it indeed necessary to keep querying for these all 
over the app? Is there a third-party solution that I've overlooked?

Django 2.0. Thank you!

-- 
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/9510552d-9afe-47b0-842e-6f80e9a28bbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: trying to learn custom validators

2018-07-17 Thread clavierplayer
Oh, this is perfect! Works exactly the way I need it. Thank you so much for 
your help!

On Monday, July 16, 2018 at 5:12:40 PM UTC-4, Matthew Pava wrote:
>
> What I would do in that situation is add a custom field to the model form 
> that holds the item number.  Then, in the save method, convert that to the 
> Item object.
>
>  
>
> class AddItem(forms.ModelForm):
>
>  
>
> item_number = IntegerField()
>
>  
>
> class Meta:
>
> model = ItemsTestCollection
>
> fields = ['qty', 'case']
>
>  
>
> def save(self, *args, **kwargs):
>
>   instance = super().save(commit=False)
>
> instance.item = 
> Item.objects.get(number=self.cleaned_data.get(‘item_number’))
>
> instance.save()
>
> return instance
>
>  
>
>  
>
>  
>
> *From:* django...@googlegroups.com  [mailto:
> django...@googlegroups.com ] *On Behalf Of *
> clavie...@gmail.com 
> *Sent:* Monday, July 16, 2018 3:02 PM
> *To:* Django users
> *Subject:* trying to learn custom validators
>
>  
>
> I have a legacy database that we want to replace, and Django is still new 
> to us. There are some 40,000 items in the old db, and each item has an 
> item_number that is not necessarily the primary key; that way, the users 
> can maintain their own sku number system and we can maintain data integrity 
> without either of us getting in the other's way. And it also helps 
> guarantee data integrity as we migrate the data from the old db to our new 
> db, since Django will assign PK's and manage FKs as it goes, and leave the 
> item_numbers intact. 
>
>  
>
> I'm running into trouble though, because Django Views and Forms all assume 
> that, when performing a lookup, the input is going to be a primary key. For 
> example, I have an item where item.item_number = '515874'. So when I put 
> that item_number in, Django looks for an item where item.pk = 515874 and 
> raises a ValidationError--because there is no such item. The actual PK for 
> item '515874' is 41293. So I need to take the user's input, use it to get 
> the right item based on its item_number, and give it back to Django to use 
> instead. 
>
>  
>
> CreateView and {{ form.as_table }} default to populating a dropdown, which 
> would mean that the whole item_num/pk problem would go away... but with 
> Django querying *all* 40,000 items, the page literally took 20 seconds to 
> load. So the users are going to need to type in item_numbers which I'll 
> have to convert to PKs. 
>
>  
>
> Overriding form_valid() and clean() weren't working, because the 
> no-such-item ValidationError had already been raised before either method 
> could be called. Defining a custom field and overriding that field's 
> validate method seems to be the right way to go, but I'm still running into 
> issues. The validate method is being called and is returning the correct 
> value, but Django seems to be throwing it away. When I step through it with 
> the debugger, I see the item where (item.id = 41293 and item.item_number 
> = '515874') is correctly being returned. But when Django calls clean() 
> next, that item is nowhere to be found and it's busy cleaning '515874', 
> which was the original input. And then it breaks.
>
>  
>
> This particular view/ModelForm is a simple tool for non-programmers to be 
> able to evaluate a small part of a background process.
>
>  
>
> class ItemsTestCollection(models.Model):
> 
> *"""An item to be tested in Box Opt."""*item = 
> models.ForeignKey('item.Item', on_delete=models.CASCADE)
> qty = models.IntegerField()
> case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
> box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)
>
>  
>
> class ItemNumField(forms.CharField):
>
> def validate(self, value):
> 
>
>
> *# item = Item.objects.get(item_number=value)# value = item.id 
> # return value*return 
> Item.objects.get(item_number=value)
>
> class AddItem(forms.ModelForm):
>
> item = ItemNumField()
>
> class Meta:
> model = ItemsTestCollection
> fields = ['item', 'qty', 'case']
>
>  
>
> class AddItemsToEvaluateCreateView(CreateView):
> model = ItemsTestCollection
> form_class = AddItem
> template_name = 'utils\evaluate.html'
>
> def get_queryset(self):
> return ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])
>
> def get_object(self, queryset=None):
> return EvaluateTestCase.objects.get(pk=self.kwargs['pk'])
>
> def get_success_url(self):
> if 'add_more' in self.request.POST:
> return reverse('utils:evaluate', kwargs={'pk': self.kwargs['pk']})
> elif 'optimize' in self.request.POST:
> return reverse('utils:results', kwargs={'pk': self.kwargs['pk']})
> else:
> return '/'
>
> def get_context_data(self, **kwargs):
> context = super().get_context_data()
> context['case_id'] = 

trying to learn custom validators

2018-07-16 Thread clavierplayer
I have a legacy database that we want to replace, and Django is still new 
to us. There are some 40,000 items in the old db, and each item has an 
item_number that is not necessarily the primary key; that way, the users 
can maintain their own sku number system and we can maintain data integrity 
without either of us getting in the other's way. And it also helps 
guarantee data integrity as we migrate the data from the old db to our new 
db, since Django will assign PK's and manage FKs as it goes, and leave the 
item_numbers intact. 

I'm running into trouble though, because Django Views and Forms all assume 
that, when performing a lookup, the input is going to be a primary key. For 
example, I have an item where item.item_number = '515874'. So when I put 
that item_number in, Django looks for an item where item.pk = 515874 and 
raises a ValidationError--because there is no such item. The actual PK for 
item '515874' is 41293. So I need to take the user's input, use it to get 
the right item based on its item_number, and give it back to Django to use 
instead. 

CreateView and {{ form.as_table }} default to populating a dropdown, which 
would mean that the whole item_num/pk problem would go away... but with 
Django querying *all* 40,000 items, the page literally took 20 seconds to 
load. So the users are going to need to type in item_numbers which I'll 
have to convert to PKs. 

Overriding form_valid() and clean() weren't working, because the 
no-such-item ValidationError had already been raised before either method 
could be called. Defining a custom field and overriding that field's 
validate method seems to be the right way to go, but I'm still running into 
issues. The validate method is being called and is returning the correct 
value, but Django seems to be throwing it away. When I step through it with 
the debugger, I see the item where (item.id = 41293 and item.item_number = 
'515874') is correctly being returned. But when Django calls clean() next, 
that item is nowhere to be found and it's busy cleaning '515874', which was 
the original input. And then it breaks.

This particular view/ModelForm is a simple tool for non-programmers to be 
able to evaluate a small part of a background process.

class ItemsTestCollection(models.Model):
"""An item to be tested in Box Opt."""
item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
qty = models.IntegerField()
case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)


class ItemNumField(forms.CharField):

def validate(self, value):
# item = Item.objects.get(item_number=value)
# value = item.id
# return value
return Item.objects.get(item_number=value)

class AddItem(forms.ModelForm):

item = ItemNumField()

class Meta:
model = ItemsTestCollection
fields = ['item', 'qty', 'case']


class AddItemsToEvaluateCreateView(CreateView):
model = ItemsTestCollection
form_class = AddItem
template_name = 'utils\evaluate.html'

def get_queryset(self):
return ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])

def get_object(self, queryset=None):
return EvaluateTestCase.objects.get(pk=self.kwargs['pk'])

def get_success_url(self):
if 'add_more' in self.request.POST:
return reverse('utils:evaluate', kwargs={'pk': self.kwargs['pk']})
elif 'optimize' in self.request.POST:
return reverse('utils:results', kwargs={'pk': self.kwargs['pk']})
else:
return '/'

def get_context_data(self, **kwargs):
context = super().get_context_data()
context['case_id'] = self.kwargs['pk']
context['items_in_order'] = 
ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])
return context


ValueError at /utils/box-optimization/add-items/118

Cannot assign "'515874'": "ItemsTestCollection.item" must be a "Item" instance.

Request Method: POST
Request URL: http://localhost:8000/utils/box-optimization/add-items/118
Django Version: 2.0.5
Exception Type: ValueError
Exception Value: 

Cannot assign "'515874'": "ItemsTestCollection.item" must be a "Item" instance.

Exception Location: 
C:\miniconda3\envs\django\lib\site-packages\django\db\models\fields\related_descriptors.py
 
in __set__, line 197
Python Executable: C:\miniconda3\envs\django\python.exe
Python Version: 3.6.5
Python Path: 

['C:\\WMS Repository\\Warehouse Management System',
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
 'C:\\WMS Repository\\Warehouse Management System',
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
 'C:\\Users\\heast\\.PyCharm2018.1\\system\\cythonExtensions',
 'C:\\miniconda3\\envs\\django\\python36.zip',
 'C:\\miniconda3\\envs\\django\\DLLs',
 'C:\\miniconda3\\envs\\django\\lib',
 'C:\\miniconda3\\envs\\django',
 

Re: help with get_form_kwargs

2018-07-16 Thread clavierplayer
Ah, thank you for the link; it put me on the right track, I think, with 
custom validators. 

As an example of why I need to customize the item fetching: I have an item 
that has an item_number of '515874'. That number is how the users interact 
with the item. However, the primary key for Item '515874' is 41293, which 
the users don't know anything about. That way they can maintain their own 
internal numbering/sku system, and we don't have to worry about data 
integrity when they do what they need to with them. So since Django is 
looking for a primary key of 515874 and not finding it, it raises a 
ValidationError and gives up before I can get to it to tell it what the pk 
really is; defining my own field solved that. 

But Django is still doing something automagically, because I changed the 
value that it passed through my validators, returned that value, and then 
Django starts cleaning it... but it's trying to clean the original  
'515874', not 41293, which is the value my validator returned. (I might 
need to start a new post simply because the topic has changed so much.) 
Thanks so much!

class ItemNumField(forms.CharField):

def validate(self, value):
# item = Item.objects.get(item_number=value)
# value = item.id
# return value
return Item.objects.get(item_number=value)

class AddItem(forms.ModelForm):

item = ItemNumField()

class Meta:
model = ItemsTestCollection
# fields = ['item', 'qty', 'case']
fields = ['item', 'qty', 'case']


class ItemsTestCollection(models.Model):
"""An item to be tested in Box Opt."""
item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
qty = models.IntegerField()
case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)




ValueError at /utils/box-optimization/add-items/118

Cannot assign "'515874'": "ItemsTestCollection.item" must be a "Item" instance.

Request Method: POST
Request URL: http://localhost:8000/utils/box-optimization/add-items/118
Django Version: 2.0.5
Exception Type: ValueError
Exception Value: 

Cannot assign "'515874'": "ItemsTestCollection.item" must be a "Item" instance.

Exception Location: 
C:\miniconda3\envs\django\lib\site-packages\django\db\models\fields\related_descriptors.py
 
in __set__, line 197
Python Executable: C:\miniconda3\envs\django\python.exe
Python Version: 3.6.5
Python Path: 

['C:\\WMS Repository\\Warehouse Management System',
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
 'C:\\WMS Repository\\Warehouse Management System',
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
 'C:\\Users\\heast\\.PyCharm2018.1\\system\\cythonExtensions',
 'C:\\miniconda3\\envs\\django\\python36.zip',
 'C:\\miniconda3\\envs\\django\\DLLs',
 'C:\\miniconda3\\envs\\django\\lib',
 'C:\\miniconda3\\envs\\django',
 'C:\\Users\\heast\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'C:\\miniconda3\\envs\\django\\lib\\site-packages',
 'C:\\Program Files\\JetBrains\\PyCharm '
 '2017.3.3\\helpers\\pycharm_matplotlib_backend']

Server time: Mon, 16 Jul 2018 14:58:30 -0400
Environment:


Request Method: POST
Request URL: http://localhost:8000/utils/box-optimization/add-items/118

Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['item.apps.ItemConfig',
 'inventory.apps.InventoryConfig',
 'fulfillment.apps.FulfillmentConfig',
 'manifest.apps.ManifestConfig',
 'order.apps.OrderConfig',
 'utils.apps.UtilsConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File 
"C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\exception.py" 
in inner
  35. response = get_response(request)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" 
in _get_response
  128. response = self.process_exception_by_middleware(e, 
request)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" 
in _get_response
  126. response = wrapped_callback(request, *callback_args, 
**callback_kwargs)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" 
in view
  69. return self.dispatch(request, *args, **kwargs)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" 
in dispatch
  89. return handler(request, *args, **kwargs)

File 

Re: help with get_form_kwargs

2018-07-16 Thread clavierplayer
I would actually prefer to override clean(), but CreateView doesn't appear 
to have a clean() method that I can override. I did make a ModelForm to use 
with the CreateView, but the clean_item method I defined there never gets 
called--breakpoints and print statements are all skipped, so I don't even 
have any stack trace information because nothing actually breaks. I've 
looked through the source code and I still don't see how CreateView cleans 
its data. 

class AddItem(forms.ModelForm):

class Meta:
model = ItemsTestCollection
fields = ['item', 'qty', 'case']

def clean_item(self):
print("will it print anything?")
# Answer: nope, didn't print anything.
item_num = self.cleaned_data['item']
actual_item = Item.objects.get(item_number=item_num)
return actual_item.id


I'm sure I've overlooked something simple, but meanwhile I'm looking for 
any kind of workaround. But thank you for the suggestion! I wish I knew how 
to implement it. 

On Monday, July 16, 2018 at 11:46:39 AM UTC-4, Matthew Pava wrote:
>
> I would alter user input in the clean methods.
>
> https://docs.djangoproject.com/en/2.0/ref/forms/validation/
>
>  
>
>  
>
> *From:* django...@googlegroups.com  [mailto:
> django...@googlegroups.com ] *On Behalf Of *
> clavie...@gmail.com 
> *Sent:* Monday, July 16, 2018 10:28 AM
> *To:* Django users
> *Subject:* help with get_form_kwargs
>
>  
>
> I have a CreateView in which I need to alter the user input. I may have 
> found a way to do so using get_form_kwargs, but I'm puzzled by a couple of 
> things:
>
>  
>
> I know that kwargs['data'] is a QueryDict, which is immutable. So I tried 
> the doc's QueryDict.copy() recommendation: 
>
> def get_form_kwargs(self):
> kwargs = super().get_form_kwargs()
> clone = kwargs['data'].copy()
> 
> *# clone = copy(kwargs['data'])*item_num = clone['item']
> actual_item = Item.objects.get(item_number=item_num)
> clone['item'] = str(actual_item.id)
> 
> *# data.update({'item': actual_item})*kwargs['data'] = clone
> return kwargs
>
>  
>
> It appears to work just fine until the return statement. I can step 
> through every line and see the values change exactly the way I want them to 
> change. By the time I get to the return statement, `clone`--which is a copy 
> of kwargs['data']--is a QueryDict that contains the correct information. 
> However, the program crashes with a KeyError after `kwargs` is returned, 
> saying that kwargs['data'] doesn't exist. In the following stack trace, 
> line 62 is the second line of the method (clone = kwargs['data'].copy()). 
>
>  
> KeyError at /utils/box-optimization/add-items/115 
>
> 'data'
>
> *Request Method:*
>
> GET
>
> *Request URL:*
>
> http://localhost:8000/utils/box-optimization/add-items/115
>
> *Django Version:*
>
> 2.0.5
>
> *Exception Type:*
>
> KeyError
>
> *Exception Value:*
>
> 'data'
>
> *Exception Location:*
>
> C:\WMS Repository\Warehouse Management System\utils\views.py in 
> get_form_kwargs, line 62
>
> *Python Executable:*
>
> C:\miniconda3\envs\django\python.exe
>
> *Python Version:*
>
> 3.6.5
>
> *Python Path:*
>
> ['C:\\WMS Repository\\Warehouse Management System',
>
>  'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
>
>  'C:\\WMS Repository\\Warehouse Management System',
>
>  'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
>
>  'C:\\Users\\heast\\.PyCharm2018.1\\system\\cythonExtensions',
>
>  'C:\\miniconda3\\envs\\django\\python36.zip',
>
>  'C:\\miniconda3\\envs\\django\\DLLs',
>
>  'C:\\miniconda3\\envs\\django\\lib',
>
>  'C:\\miniconda3\\envs\\django',
>
>  'C:\\Users\\heast\\AppData\\Roaming\\Python\\Python36\\site-packages',
>
>  'C:\\miniconda3\\envs\\django\\lib\\site-packages',
>
>  'C:\\Program Files\\JetBrains\\PyCharm '
>
>  '2017.3.3\\helpers\\pycharm_matplotlib_backend']
>
> *Server time:*
>
> Mon, 16 Jul 2018 11:16:38 -0400
>
> Environment:
>
>  
>
>  
>
> Request Method: GET
>
> Request URL: http://localhost:8000/utils/box-optimization/add-items/115
>
>  
>
> Django Version: 2.0.5
>
> Python Version: 3.6.5
>
> Installed Applications:
>
> ['item.apps.ItemConfig',
>
>  'inventory.apps.InventoryConfig',
>
>  'fulfillment.apps.FulfillmentConfig',
>
>  'manifest.apps.ManifestConfig',
>
>  'order.apps.OrderConfig',
>
>  'utils.apps.UtilsConfig',
>
>  'django.contrib.admin',
>
>  'django.contrib.auth',
>
>  'django.contrib.contenttypes',
>
>  'django.contrib.sessions',
>
>  'django.contrib.messages',
>
>  'django.contrib.staticfiles']
>
> Installed Middleware:
>
> ['django.middleware.security.SecurityMiddleware',
>
>  'django.contrib.sessions.middleware.SessionMiddleware',
>
>  'django.middleware.common.CommonMiddleware',
>
>  'django.middleware.csrf.CsrfViewMiddleware',
>
>  'django.contrib.auth.middleware.AuthenticationMiddleware',
>
>  'django.contrib.messages.middleware.MessageMiddleware',
>
>  'django.middleware.clickjacking.XFrameOptionsMiddleware']
>
>  

help with get_form_kwargs

2018-07-16 Thread clavierplayer
I have a CreateView in which I need to alter the user input. I may have 
found a way to do so using get_form_kwargs, but I'm puzzled by a couple of 
things:

I know that kwargs['data'] is a QueryDict, which is immutable. So I tried 
the doc's QueryDict.copy() recommendation: 

def get_form_kwargs(self):
kwargs = super().get_form_kwargs()
clone = kwargs['data'].copy()
# clone = copy(kwargs['data'])
item_num = clone['item']
actual_item = Item.objects.get(item_number=item_num)
clone['item'] = str(actual_item.id)
# data.update({'item': actual_item})
kwargs['data'] = clone
return kwargs


It appears to work just fine until the return statement. I can step through 
every line and see the values change exactly the way I want them to change. 
By the time I get to the return statement, `clone`--which is a copy of 
kwargs['data']--is a QueryDict that contains the correct information. 
However, the program crashes with a KeyError after `kwargs` is returned, 
saying that kwargs['data'] doesn't exist. In the following stack trace, 
line 62 is the second line of the method (clone = kwargs['data'].copy()). 

KeyError at /utils/box-optimization/add-items/115

'data'

Request Method: GET
Request URL: http://localhost:8000/utils/box-optimization/add-items/115
Django Version: 2.0.5
Exception Type: KeyError
Exception Value: 

'data'

Exception Location: C:\WMS Repository\Warehouse Management 
System\utils\views.py in get_form_kwargs, line 62
Python Executable: C:\miniconda3\envs\django\python.exe
Python Version: 3.6.5
Python Path: 

['C:\\WMS Repository\\Warehouse Management System',
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
 'C:\\WMS Repository\\Warehouse Management System',
 'C:\\Program Files\\JetBrains\\PyCharm 2017.3.3\\helpers\\pydev',
 'C:\\Users\\heast\\.PyCharm2018.1\\system\\cythonExtensions',
 'C:\\miniconda3\\envs\\django\\python36.zip',
 'C:\\miniconda3\\envs\\django\\DLLs',
 'C:\\miniconda3\\envs\\django\\lib',
 'C:\\miniconda3\\envs\\django',
 'C:\\Users\\heast\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'C:\\miniconda3\\envs\\django\\lib\\site-packages',
 'C:\\Program Files\\JetBrains\\PyCharm '
 '2017.3.3\\helpers\\pycharm_matplotlib_backend']

Server time: Mon, 16 Jul 2018 11:16:38 -0400
Environment:


Request Method: GET
Request URL: http://localhost:8000/utils/box-optimization/add-items/115

Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['item.apps.ItemConfig',
 'inventory.apps.InventoryConfig',
 'fulfillment.apps.FulfillmentConfig',
 'manifest.apps.ManifestConfig',
 'order.apps.OrderConfig',
 'utils.apps.UtilsConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File 
"C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\exception.py" 
in inner
  35. response = get_response(request)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" 
in _get_response
  128. response = self.process_exception_by_middleware(e, 
request)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\core\handlers\base.py" 
in _get_response
  126. response = wrapped_callback(request, *callback_args, 
**callback_kwargs)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" 
in view
  69. return self.dispatch(request, *args, **kwargs)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\base.py" 
in dispatch
  89. return handler(request, *args, **kwargs)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" 
in get
  168. return super().get(request, *args, **kwargs)

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" 
in get
  133. return self.render_to_response(self.get_context_data())

File "C:\WMS Repository\Warehouse Management System\utils\views.py" in 
get_context_data
  93. context = super().get_context_data()

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" 
in get_context_data
  66. kwargs['form'] = self.get_form()

File 
"C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py" 
in get_form
  33. return form_class(**self.get_form_kwargs())

File "C:\WMS Repository\Warehouse Management System\utils\views.py" in 
get_form_kwargs
  62. clone = kwargs['data'].copy()

Exception Type: KeyError at 

trouble with forms/CreateView

2018-07-16 Thread clavierplayer
I seem to be having trouble understanding some of how form validation 
works, resulting in two problems. 

I am trying to write a CreateView in which a user can type in an item 
number, and the program will use that instead of the item's primary key to 
perform the lookup. 

The docs appear to recommend overriding form_valid, but these are the 
issues I've been running into: 

   1. I would need duplicate code in both form_valid and form_invalid 
   because the item number entered by the user may or may not also happen to 
   be a primary key for a different item. As far as I've seen, Django assumes 
   that the input is a primary key, and will call either method as appropriate 
   based on whether or not it could find an item with that primary key.
   2. Overriding form_invalid doesn't seem to be working anyway:
   - Django appears to clean data before validating it, and I believe 
  cleaned data is immutable. 
  - CreateView has no clean() method to override that I can find 
  (either here 
  
 
or 
  the source code itself on github)
  - I made a ModelForm to use with CreateView expressly for the clean() 
  methods it offers, but neither clean() nor clean_ are being called 
  (my breakpoints are being skipped). 
   
Does anybody know a way to accomplish this? How does CreateView clean its 
data? Any suggestions would be helpful.

Thank you!
Heather

-- 
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/af2a159b-5ba8-4939-bf48-c89ba7ebd710%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


ModelForm: overriding is_valid()

2018-07-13 Thread clavierplayer
I have an Item model. It has both a primary key and an item number; the 
users refer to item numbers when finding things, and the actual primary key 
is not exposed. I'm trying to write a CreateView that will allow the user 
to input the item number. 

I see that CreateView defaults to using the primary key to find model 
objects, which any other time makes sense. I'm thinking the solution is to 
use a ModelForm, but I'm stuck on the same problem--I'm not sure how to 
accept a string from the user (instead of the primary key), do my own 
query, and fetch the required model object that way. I think overriding 
is_valid() is the correct way to go, but I'm not sure what I'm leaving 
out--when I override this method, the data is never committed to the 
database even though I call super(). I've also tried overriding save() and 
clean() on the ModelForm, but with the same results (that is, no data in 
the database). 

I'm writing a simple app that uses Item as a foreign key: 

class ItemsTestCollection(models.Model):
"""An item to be tested in Box Opt."""
item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
qty = models.IntegerField()
case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)

...using this CreateView:

class AddItemsToEvaluateCreateView(CreateView):
model = ItemsTestCollection
form_class = AddItem
# fields = ['item', 'qty', 'case']
template_name = 'utils\evaluate.html'


# Not working...
def form_valid(self, form):
data = form.save(commit=False)
item_num = data.item
data.item = Item.objects.get(item_number=item_num)
data.save()

return self.get_success_url()


...and this ModelForm:

class AddItem(forms.ModelForm):

class Meta:
model = ItemsTestCollection
fields = ['item', 'qty', 'case']
# widgets = {'item': CharField(max_length=6)}

# item = forms.CharField(max_length=6)
def is_valid(self):
item_num = self.data['item']  # 
# self.data['item'] = Item.objects.get(item_number=item_num)
self.instance.item = Item.objects.get(item_number=item_num)
# self.instance.qty = self.data['qty']
# self.instance.case = 
EvaluateTestCase.objects.get(pk=self.data['case'])
super().is_valid()


And I somehow need to refer to the Item by item_number and not the 
auto-assigned pk:

class Item(models.Model):
"""Item attributes"""

item_number = models.CharField(max_length=6, null=True, unique=True)



Thank you!

-- 
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/5f4a4128-71f4-482a-b443-affe42fee109%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: formaction vs get_absolute_url

2018-07-13 Thread clavierplayer
Thank you for the clear explanation! It works beautifully now.

On Friday, July 13, 2018 at 9:17:37 AM UTC-4, Daniel Roseman wrote:
>
> On Thursday, 12 July 2018 21:04:25 UTC+1, clavie...@gmail.com wrote:
>>
>> I'm looking for a way to redirect to a different page depending upon 
>> which submit button the user clicks. I included a formaction tag in the 
>> template, but django still demands that get_absolute_url be defined. Using 
>> Django 2.0
>>
>> Basically, I'm trying to write a small app that launches a test case, 
>> which would allow a non-programmer to evaluate a small section of code 
>> responsible for a background process. The app should allow the user to add 
>> as many test items to it as they wish, one at a time. When an item is 
>> added, they can choose to click and 'evaluate' button or an 'add more 
>> items' button, and each button should go to a different page--basically, 
>> 'add more items' should refresh the same page. Clicking on 'add more items' 
>> does indeed post the data to the database, but it ignores the formaction 
>> tag. The stack trace says that form_valid is calling get_absolute_url on 
>> the model (which I have not defined), but the target url has to do with 
>> what the user wants to do next with said model, not the model itself. I 
>> haven't figured out how to override the get_absolute_url call based on 
>> which html button was clicked. (Though it also said I can provide an url, 
>> which I did on the template...)
>>
>> (The code I'm pasting below may be a little awkward because I've been 
>> finding workarounds for things by trial and error; so if anything 
>> particularly egregious catches someone's eye, comments to that end are 
>> welcome.)
>>
>
>
> You've misdiagnosed the problem. formaction is working fine; the data is 
> being posted to your add-items view. The issue is what happens next. Any 
> successful POST should be followed by a redirect, and CreateView by default 
> uses the value of get_absolute_url to determine where to direct to.
>
> Actually, formaction isn't what you want here at all. You've almost 
> mentioned the solution by talking about overriding get_absolute_url based 
> on the HTML button; that's not quite it, what you actually need to do is to 
> override the method responsible for calling it, which is the view's 
> get_success_url.
>
> class AddItemsToEvaluateCreateView(CreateView):
> ...
> def get_success_url(self):
> if 'add_more' in self.request.POST:
> return reverse('evaluate', kwargs={'pk': self.kwargs['pk']}
> elif 'optimize' in self.request.POST:
> return reverse('results', kwargs={'pk': self.kwargs['pk']}
> return '/'
>
>
> Also you'll need to change your input elements; they don't need 
> `formaction`, but they do need a `name` so that they are sent in the POST 
> data:
>
> 
> 
>
> --
> DR.
>
>>

-- 
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/a47aef1d-0dcd-4d35-9f0f-aaa0ba43c8ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: formaction vs get_absolute_url

2018-07-12 Thread clavierplayer
Relevant urls:

app_name = "utils"
urlpatterns = [
path('', index, name="index"),
path('', index, name="index"),
path('migrate/', migrate, name="migrate"),

# -- Box Opt Evaluation urls --
path('box-optimization/instructions', instruct, name='instructions'),
path('box-optimization/add-items/', 
AddItemsToEvaluateCreateView.as_view(), name='evaluate'),
path('box-optimization/new-test-case', create_new_test_case, 
name='new-case'),
path('box-optimization/results/', 
EvaluateResultsUpdateView.as_view(), name='results')
]


On Thursday, July 12, 2018 at 4:04:25 PM UTC-4, clavie...@gmail.com wrote:
>
> I'm looking for a way to redirect to a different page depending upon which 
> submit button the user clicks. I included a formaction tag in the template, 
> but django still demands that get_absolute_url be defined. Using Django 2.0
>
> Basically, I'm trying to write a small app that launches a test case, 
> which would allow a non-programmer to evaluate a small section of code 
> responsible for a background process. The app should allow the user to add 
> as many test items to it as they wish, one at a time. When an item is 
> added, they can choose to click and 'evaluate' button or an 'add more 
> items' button, and each button should go to a different page--basically, 
> 'add more items' should refresh the same page. Clicking on 'add more items' 
> does indeed post the data to the database, but it ignores the formaction 
> tag. The stack trace says that form_valid is calling get_absolute_url on 
> the model (which I have not defined), but the target url has to do with 
> what the user wants to do next with said model, not the model itself. I 
> haven't figured out how to override the get_absolute_url call based on 
> which html button was clicked. (Though it also said I can provide an url, 
> which I did on the template...)
>
> (The code I'm pasting below may be a little awkward because I've been 
> finding workarounds for things by trial and error; so if anything 
> particularly egregious catches someone's eye, comments to that end are 
> welcome.)
>
> Problem view:
>
> class AddItemsToEvaluateCreateView(CreateView, FormMixin):
> model = ItemsTestCollection
> form_class = EnterItem
> template_name = 'utils\evaluate.html'
>
> def get_queryset(self):
> return ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])
>
> def get_object(self, queryset=None):
> return EvaluateTestCase.objects.get(pk=self.kwargs['pk'])
>
> def get_context_data(self, **kwargs):
> context = super().get_context_data()
> context['case_id'] = self.kwargs['pk']
> context['items_in_order'] = 
> ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])
> return context
>
>
> Problem models:
>
> class ItemsTestCollection(models.Model):
> """An item to be tested in Box Opt."""
> item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
> qty = models.IntegerField()
> case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
> box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)
>
>
> class EvaluateTestCase(models.Model):
> """Individual test cases."""
>
> STATUS = (
> ('no', 'Something bad happened,'),
> ('hm', 'Needs improvement.'),
> ('ok', 'Seems all right.')
> )
>
> verdict = models.CharField(max_length=2, choices=STATUS, blank=False, 
> null=True)
> feedback = models.TextField(blank=True, null=True)
>
> def get_absolute_url(self):
> return reverse('utils:evaluate', kwargs={'pk': str(self.id)})
>
> @staticmethod
> def get_item_lots(case_number: int):
> """Get the first ItemLot for each item in the EvaluateTestCase."""
> item_lots: List[ItemLot] = []
> for item_collection in 
> ItemsTestCollection.objects.filter(case_id=case_number):
> 
> item_lots.append(ItemLot.objects.filter(item=item_collection.item)[0])
>
> return item_lots
>
>
>
> Problem template:
>
> 
> {% csrf_token %}
> 
> 
> Item #: 
> 
> 
> 
> Qty: 
> 
> 
> 
> Case: 
> 
> 
> 
>  value="Add more items" />
>  value="Optimize" />
> 
>
>
>
> Error:
> ImproperlyConfigured at /utils/box-optimization/add-items/75
>
> No URL to redirect to.  Either provide a url or define a get_absolute_url 
> method on the Model.
>
> Request Method: POST
> Request URL: http://localhost:8000/utils/box-optimization/add-items/75
> Django Version: 2.0.5
> Exception Type: ImproperlyConfigured
> Exception Value: 
>
> No URL to redirect to.  Either provide a url or define a get_absolute_url 
> method on the Model.
>
> Exception Location: 
> C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py 
> in get_success_url, line 119
> Python Executable: 

formaction vs get_absolute_url

2018-07-12 Thread clavierplayer
I'm looking for a way to redirect to a different page depending upon which 
submit button the user clicks. I included a formaction tag in the template, 
but django still demands that get_absolute_url be defined. Using Django 2.0

Basically, I'm trying to write a small app that launches a test case, which 
would allow a non-programmer to evaluate a small section of code 
responsible for a background process. The app should allow the user to add 
as many test items to it as they wish, one at a time. When an item is 
added, they can choose to click and 'evaluate' button or an 'add more 
items' button, and each button should go to a different page--basically, 
'add more items' should refresh the same page. Clicking on 'add more items' 
does indeed post the data to the database, but it ignores the formaction 
tag. The stack trace says that form_valid is calling get_absolute_url on 
the model (which I have not defined), but the target url has to do with 
what the user wants to do next with said model, not the model itself. I 
haven't figured out how to override the get_absolute_url call based on 
which html button was clicked. (Though it also said I can provide an url, 
which I did on the template...)

(The code I'm pasting below may be a little awkward because I've been 
finding workarounds for things by trial and error; so if anything 
particularly egregious catches someone's eye, comments to that end are 
welcome.)

Problem view:

class AddItemsToEvaluateCreateView(CreateView, FormMixin):
model = ItemsTestCollection
form_class = EnterItem
template_name = 'utils\evaluate.html'

def get_queryset(self):
return ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])

def get_object(self, queryset=None):
return EvaluateTestCase.objects.get(pk=self.kwargs['pk'])

def get_context_data(self, **kwargs):
context = super().get_context_data()
context['case_id'] = self.kwargs['pk']
context['items_in_order'] = 
ItemsTestCollection.objects.filter(case_id=self.kwargs['pk'])
return context


Problem models:

class ItemsTestCollection(models.Model):
"""An item to be tested in Box Opt."""
item = models.ForeignKey('item.Item', on_delete=models.CASCADE)
qty = models.IntegerField()
case = models.ForeignKey('EvaluateTestCase', on_delete=models.CASCADE)
box = models.ForeignKey('BoxResults', on_delete=models.CASCADE, null=True)


class EvaluateTestCase(models.Model):
"""Individual test cases."""

STATUS = (
('no', 'Something bad happened,'),
('hm', 'Needs improvement.'),
('ok', 'Seems all right.')
)

verdict = models.CharField(max_length=2, choices=STATUS, blank=False, 
null=True)
feedback = models.TextField(blank=True, null=True)

def get_absolute_url(self):
return reverse('utils:evaluate', kwargs={'pk': str(self.id)})

@staticmethod
def get_item_lots(case_number: int):
"""Get the first ItemLot for each item in the EvaluateTestCase."""
item_lots: List[ItemLot] = []
for item_collection in 
ItemsTestCollection.objects.filter(case_id=case_number):

item_lots.append(ItemLot.objects.filter(item=item_collection.item)[0])

return item_lots



Problem template:


{% csrf_token %}


Item #: 



Qty: 



Case: 









Error:
ImproperlyConfigured at /utils/box-optimization/add-items/75

No URL to redirect to.  Either provide a url or define a get_absolute_url 
method on the Model.

Request Method: POST
Request URL: http://localhost:8000/utils/box-optimization/add-items/75
Django Version: 2.0.5
Exception Type: ImproperlyConfigured
Exception Value: 

No URL to redirect to.  Either provide a url or define a get_absolute_url 
method on the Model.

Exception Location: 
C:\miniconda3\envs\django\lib\site-packages\django\views\generic\edit.py 
in get_success_url, line 119
Python Executable: C:\miniconda3\envs\django\python.exe
Python Version: 3.6.5
Python Path: 

['C:\\WMS Repository\\Warehouse Management System',
 'C:\\WMS Repository\\Warehouse Management System',
 'C:\\miniconda3\\envs\\django\\python36.zip',
 'C:\\miniconda3\\envs\\django\\DLLs',
 'C:\\miniconda3\\envs\\django\\lib',
 'C:\\miniconda3\\envs\\django',
 'C:\\Users\\heast\\AppData\\Roaming\\Python\\Python36\\site-packages',
 'C:\\miniconda3\\envs\\django\\lib\\site-packages',
 'C:\\Program Files\\JetBrains\\PyCharm '
 '2017.3.3\\helpers\\pycharm_matplotlib_backend']

Server time: Thu, 12 Jul 2018 15:47:04 -0400

Environment:


Request Method: POST
Request URL: http://localhost:8000/utils/box-optimization/add-items/75

Django Version: 2.0.5
Python Version: 3.6.5
Installed Applications:
['item.apps.ItemConfig',
 'inventory.apps.InventoryConfig',
 'fulfillment.apps.FulfillmentConfig',
 'manifest.apps.ManifestConfig',
 'order.apps.OrderConfig',
 

Re: tests pass individually but fail together

2018-07-06 Thread clavierplayer
Hi Melvyn,

I've been working on getting permission to post the code, but everybody who 
can do that is on vacation at present, unfortunately. I'm hoping somebody 
will be back to work on Monday. I did try the test --parallel 1, but that 
didn't help.

The problem tests all cover a particular method, and only the first test to 
run ever passes. Today I wrote a reset method to call at the beginning of 
each test, and *that* tests out just fine... by itself. I tested it by 
first calling the other method that seems to be causing issues, then 
calling the reset method and asserting that all of the records did indeed 
reset. But when called as a suite, that test fails, too.

I suppose for now I can skip some of the data in setUpTestData and just 
create brand new records for each test.

Thank you!
Heather

On Thursday, July 5, 2018 at 3:19:48 PM UTC-4, Melvyn Sopacua wrote:
>
> On donderdag 5 juli 2018 18:07:58 CEST clavie...@gmail.com  
> wrote: 
> > It's a script that's supposed to run in the background for an inventory 
> > management program. The basic idea is that it periodically collects 
> Order 
> > objects that are associated with a particular Status (1-M foreign key, 
> > though more like 1-1 in practice). Orders have one or many Orderlines. 
> Then 
> > it makes all the necessary changes to various quantity fields in various 
> > inventory tables. So if an Orderline contains a request for 3 of a 
> > particular item, then 3 of that item are reserved in inventory (or 
> > backordered or however it needs to go). So far that part has been fine. 
> > 
> > But when that's finished, the Orders and associated Orderlines need 
> updated 
> > Statuses. The assert statements say that only some of them were updated, 
> > even though I watched them all update correctly--it's like a partial 
> > rollback happens before the test finishes. The other failing test checks 
> > that the process of updating the statuses also logs the events properly 
> in 
> > a History table; but when the test is run as a suite, the event is only 
> > partially logged--some records are created and some aren't. But the test 
> > results are always consistent for the way the test was run. 
>
> My first instinct is to run  the suite with `--parallel 1` and see if that 
> changes anything. Also, it really helps to have some code - if only to see 
> how 
> the methods relate to each other if at all. 
>
> -- 
> Melvyn Sopacua 
>

-- 
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/0103df45-5800-4751-b138-b50d9f354dae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: tests pass individually but fail together

2018-07-05 Thread clavierplayer
It's a script that's supposed to run in the background for an inventory 
management program. The basic idea is that it periodically collects Order 
objects that are associated with a particular Status (1-M foreign key, 
though more like 1-1 in practice). Orders have one or many Orderlines. Then 
it makes all the necessary changes to various quantity fields in various 
inventory tables. So if an Orderline contains a request for 3 of a 
particular item, then 3 of that item are reserved in inventory (or 
backordered or however it needs to go). So far that part has been fine. 

But when that's finished, the Orders and associated Orderlines need updated 
Statuses. The assert statements say that only some of them were updated, 
even though I watched them all update correctly--it's like a partial 
rollback happens before the test finishes. The other failing test checks 
that the process of updating the statuses also logs the events properly in 
a History table; but when the test is run as a suite, the event is only 
partially logged--some records are created and some aren't. But the test 
results are always consistent for the way the test was run.

So it's just Python and SQL Server--though we do have to use pyodbc to make 
Django recognize SQL Server, so there's an extra 'layer' there. 

Thank you for the reply, Dan!
Heather


On Thursday, July 5, 2018 at 10:50:49 AM UTC-4, Dan Tagg wrote:
>
> Hi Heather,
>
> What is it you are testing? I have had issues with this kind of thing when 
> testing forms that are programatically changing dropdown list / default 
> values.
>
> Dan
>
> On 5 July 2018 at 14:24, > wrote:
>
>> Hello, I'm simplifying a previous post. I just started using Django a few 
>> months ago, and for some reason I'm still having trouble getting my mind 
>> around the way Django thinks about some things. 
>>
>> I have a test class that contains two specific tests that always pass 
>> when run just by themselves, but always fail when run with the rest of the 
>> test class. 
>>
>> One of the tests in particular is very puzzling, because I can step 
>> through the entire method under test and watch everything happen exactly 
>> the way it should--the database field values are changed correctly and the 
>> correct values are returned. But after the return statement, the values are 
>> different than they were before the return statement. They are consistently 
>> different, so *something* is happening behind the scenes... but I have no 
>> idea what... So I can't tell if my tests are flawed or if my code under 
>> test is flawed.
>>
>> Django 2.0 with MS SQL Server 2014. Any suggestions at all appreciated, 
>> because I'm completely out of ideas. I haven't been able to get 
>> TransactionTestCase to work at all, though--set-up data that works 
>> flawlessly with TestCase breaks TransactionTestCase, and I don't know why. 
>>
>> Thank you!
>> Heather
>>
>> -- 
>> 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/72497524-6c76-41de-ba70-a5b0996335dd%40googlegroups.com
>>  
>> 
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> -- 
> Wildman and Herring Limited, Registered Office: 28 Brock Street, Bath, 
> United Kingdom, BA1 2LN, Company no: 05766374
>

-- 
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/816a9e74-ff3a-40d7-a570-91d0339cc1da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


tests pass individually but fail together

2018-07-05 Thread clavierplayer
Hello, I'm simplifying a previous post. I just started using Django a few 
months ago, and for some reason I'm still having trouble getting my mind 
around the way Django thinks about some things. 

I have a test class that contains two specific tests that always pass when 
run just by themselves, but always fail when run with the rest of the test 
class. 

One of the tests in particular is very puzzling, because I can step through 
the entire method under test and watch everything happen exactly the way it 
should--the database field values are changed correctly and the correct 
values are returned. But after the return statement, the values are 
different than they were before the return statement. They are consistently 
different, so *something* is happening behind the scenes... but I have no 
idea what... So I can't tell if my tests are flawed or if my code under 
test is flawed.

Django 2.0 with MS SQL Server 2014. Any suggestions at all appreciated, 
because I'm completely out of ideas. I haven't been able to get 
TransactionTestCase to work at all, though--set-up data that works 
flawlessly with TestCase breaks TransactionTestCase, and I don't know why. 

Thank you!
Heather

-- 
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/72497524-6c76-41de-ba70-a5b0996335dd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


TestCase vs TransactionTestCase

2018-07-03 Thread clavierplayer
Hello, I'm looking for some insight for some odd behavior with some unit 
tests, which I think stems from some misunderstanding of how these classes 
actually work. 

1. For TestCase, the Django docs say that it does matter what order in 
which the tests are run. But elsewhere in the same doc, it says that 
TestCase methods are individually wrapped in a single transaction block 
that is rolled back after the test completes. So... does each test method 
start 'fresh' or not? I currently have a test method that is supposed to 
query two records, but so far it finds either 4 or 6 with no apparent 
pattern; so I don't know if it's my code that is causing problems or the 
order in which my tests are running. 

2. The strangest behavior comes from a test method that passes by itself, 
fails when run with all the others, but the code under test actually does 
seem to work. As one might expect, the section under test takes some 
records created especially for that purpose and modifies a few fields; the 
test calls the function, queries the test database, and assert-tests the 
results. The test fails, saying that only some of the fields were 
successfully changed. I thought that I perhaps forgot to call save() in one 
of my conditional branches, but I looked and found that save() was in 
indeed where it belonged. I stepped through the code and everything 
appeared to work as expected. The values looked correct. Finally, I stuck a 
query into the code under test itself immediately before the return 
statement, and printed the results; and the query proves that the database 
field values themselves did indeed change correctly. But after the return 
statement (when it goes back to the test method itself), an identical query 
gets different results. So I'm rather puzzled. I do use select_for_update 
in my section under test, which I know sometimes causes problems with 
TestCase, but all I'm doing is testing for correct results.

3. I thought subclassing TransactionTestCase instead might solve both 
problems, but set-up data that works flawlessly with TestCase breaks every 
single test with TransactionTestCase--get() complains about non-existent 
records, the db complains about broken constraints, etc. I use both 
create() and bulk_create() in setting up my data, but neither appears to 
work at all. I tried using them in setUpTestData(), setUpClass(), and 
setUp(), and not a single test would run with any of them. 

The docs say that the two classes 'are identical except for the manner in 
which the database is reset to a known state', so I'm not sure where I went 
wrong.

Using Django 2.0. And unfortunately we use MS SQL Server, which probably 
isn't helping. 

Thank you!
Heather

-- 
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/671d7dc0-ce5a-4e90-9a44-70357ecd8f6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: prevent AppConfig.ready() from running twice

2018-06-25 Thread clavierplayer
Thanks so much, y'all! Very helpful!

On Sunday, June 24, 2018 at 3:47:48 AM UTC-4, Mike Dewhirst wrote:
>
> On 23/06/2018 6:17 PM, Melvyn Sopacua wrote: 
> > On zaterdag 23 juni 2018 02:01:06 CEST Mike Dewhirst wrote: 
> > 
> >> Is there a python singleton pattern which might work? 
> > No, cause the startup is done in 2 different processes which do not 
> share 
> > state. So both processes will have a "new singleton". 
> > This is why you need an IPC mechanism, such as file locks or shared 
> memory. 
>
> OK. That's very clear. Thank you Melvyn. 
>
> Cheers 
>
> Mike 
>
> >   In 
> > the case of one-off launchers, it's usually easier to implement the 
> > restrictions on the client side (the program being launched). Long 
> running 
> > launchers (like inetd, systemd) can prevent double launch in other ways 
> as 
> > they can keep their own state. 
>
>

-- 
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/5bee9c83-aec8-4a72-a9e6-1196bb989df1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


prevent AppConfig.ready() from running twice

2018-06-21 Thread clavierplayer
Hello, I am still new to Django, so I apologize if this seems like a silly 
question. I've not been able to find what I need on Google, StackOverflow, 
or the Django Docs. 

I am using Django 2.0.5.

I understand that the correct way to run startup code is to subclass 
AppConfig and override ready(). I also understand that Django launches two 
separate instances of the application--one to check the models, and the 
other to launch the server--on separate processes. The official 
recommendation to prevent startup code from being run more than once is to 
implement a flag--which can't work if there are multiple instances of the 
application being created. The docs also say that ready() will be re-called 
only rarely; but this doesn't help at all if all of the instances are 
calling ready() only once. 

I have found the manage.py runserver --noreload command, but this won't be 
enough to prevent multiple instances on a production server. What can I do 
to enforce one and only one instance of my application being run at a time? 

If it helps, here is the reason I need to override this multi-instantiation 
behavior: my application launches a multiprocessing.Process at startup to 
monitor and run background tasks. Having more than one background Process 
running at once is going to wreak havoc on the application. I've looked at 
other options to accomplish similar purposes, but those would all be 
instantiated multiple times, also. 

Any suggestions?

Thank you,
Heather

-- 
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/6d8729fe-18c4-4e53-8479-c2b9dd178b89%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.