Multiple django inlineformset_factory problem

2014-10-09 Thread Farhan Khan
Hi all,

I am writing a tool that implements NIST security controls and having 
trouble implementing inlineformset_factory, which I think is the solution 
to this problem.

Background: I have two models, one is called 'Controls' and the other is 
'Implementation'. When the user goes to a page, the views.py should query 
several relevant controls as text (not as a Form) and then an 
Implementation form immediately afterwards. This will be repeated depending 
on the number of Controls are found.

So, lets suppose there are 20 controls, you should see a table with the 
following:

{% for control in controlset %}

Control Name: "This is the control name 1" -- Presumably this comes from: {{ 
control.name.value }}
Control Description: "This is the control description 1" -- Presumably this 
comes from: {{ control.description.value }}
Implementation Statement: [Input type=text name=1_implementation] -- Not 
sure how to display this
Implementation Status: [Input type=text name=1_status] -- Not sure how to 
display this

{% endfor %}

(This is pseudo-code, removed the HTML for visibility)

This frame should be repeated depending on the number of controls, it could 
be 5, 20 or 100, then a submit button. When the user submits the data, I 
want to capture this data into several 'Implementation' objects and save 
them in the database.

My failing code currently does this:

ControlImplementationSet = inlineformset_factory(Controls, Implementation)
allControls = Controls.objects.get(pk=1)
controlset = ControlImplementationSet(instance=allControls)

Even though I want multiple forms, I used the Controls.objects.get(pk=1) to 
specify a single control. When I send the variable controlset as a context 
to be displayed as:

{{ controlset }}

I oddly get 3 forms that look like autogenerations of the Implementation 
form with an extra "Delete" option. That makes no sense to me. I don't know 
understand why this is displayed. I want to display the queried controls, 
followed by an implementation form.

My questions are:

   1. Why do I get 3 "Implementation" forms, instead of just 1, when I did 
   Controls.objects.get(pk=1), which should specify a single Control?
   2. How do I display multiple Implementation Forms for each Control that 
   is returned?
   3. How do I only display certain elements of the Implementation form, 
   not every field in the model as an HTML  field?
   4. How do I have my views.py interpret the submitted Implementation form 
   data from the user?

And last but not least...is there a sample of this? Surely I am not the 
first one who has done this, and the he documentation is not making sense 
to me.
https://docs.djangoproject.com/en/1.7/topics/forms/modelforms/#inline-formsets

Please advise, this is day two, quite frustrated.

Thanks,
Farhan

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/89495462-3f99-4b9d-86bd-61db11126b94%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Integer Form with Select Widget?

2014-10-25 Thread Farhan Khan
Hi all,

I want to get an Integer value from the user through a forms.Form. I have 
three unique requirements for it.

   1. The corresponding Widget is Select or SelectMultiple (either one)
   2. The 'choices' field is dynamic, not static or hard-coded -they are 
   adjusted upon each display of the Form based on program logic.
   3. I want the max_value and min_value of IntegerField to also be dynamic 
   - also adjusted based on program logic.
   

What I have thus far is this:

class deleteUserForm(forms.Form):
   delUserId = forms.CharField(label='delUserId', widget=forms.Select)

How would this work?

Thanks
- Farhan

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/3c12eccc-a3b8-4108-83af-8288a2ccd998%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Integer Form with Select Widget?

2014-10-25 Thread Farhan Khan
Any takers? I suspect that I need to mess around with the form during 
instantiation, but not sure how.

On Saturday, October 25, 2014 12:29:45 PM UTC-4, Farhan Khan wrote:
>
> Hi all,
>
> I want to get an Integer value from the user through a forms.Form. I have 
> three unique requirements for it.
>
>1. The corresponding Widget is Select or SelectMultiple (either one)
>2. The 'choices' field is dynamic, not static or hard-coded -they are 
>adjusted upon each display of the Form based on program logic.
>3. I want the max_value and min_value of IntegerField to also be 
>dynamic - also adjusted based on program logic.
>
>
> What I have thus far is this:
>
> class deleteUserForm(forms.Form):
>delUserId = forms.CharField(label='delUserId', widget=forms.Select)
>
> How would this work?
>
> Thanks
> - Farhan
>

-- 
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 http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/db7e9e3e-3cf2-4506-b5a4-d05f018aced8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


modelformset_factory generating "object has no attribute instance" error

2016-05-14 Thread Farhan Khan
I am trying to display a modelformset_factory in a view, but am running 
into this error and unable to figure out why.

builtins.AttributeError
AttributeError: 'IPAddressForm' object has no attribute 'instance'

This seems to happen when I include this in my view: {{ 
ipaddressformset.as_table }}
This is generated from a modelformset_factory, which is from the model 
IPAddress and form IPAddressForm. 

I do not know what is causing this error! The full stack dump from the WSGI 
driver is here, but my code dump below might be more useful:
https://gist.github.com/anonymous/ce858b89fe56929977915d0613f147c2

My view:

@login_required(login_url='/auth/')

def manualimport(request):

IPAddressFormSet = modelformset_factory(IPAddress, form=IPAddressForm, 
exclude=())

ipaddressformset = IPAddressFormSet( queryset=IPAddress.objects.none(), 
prefix='role' )

if request.method == 'POST':

assetform = AssetForm(request.POST)

print("POST request")

pass

else:

assetform = AssetForm()

context = {'full_name': request.user.username,

   'assetform': assetform,

   'ipaddressformset': ipaddressformset}

return render(request, 'asset/manualimport.html', context)

My form:

class IPAddressForm(forms.Form):

address = forms.ChoiceField(required=True)

new_address = forms.CharField(required=False)

def __init__(self, *args, **kwargs):

choices = ( ('','---'), (0,'Not Listed') )

ipaddresses = 
IPAddress.objects.filter(component__isnull=True).distinct()

for ipaddress in ipaddresses:

choices = choices + ((ipaddress.id, ipaddress.address),)

super(IPAddressForm, self).__init__(*args, **kwargs)

self.fields['address'].choices = choices

self.fields['address'].initial = ''


My template:

{{ ipaddressformset.management_form }}

{{ ipaddressformset.as_table }}


It appears to error out when the second line in the template is displayed. 
I do not know what the error is or what is causing it.

Please assist! Thanks.

PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B

-- 
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/de94b3aa-4c1e-4ad5-a0ac-3f36b3a3f780%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


AUTH_PASSWORD_VALIDATORS not used when creating new users?

2016-06-27 Thread Farhan Khan
Hi all,

I set the AUTH_PASSWORD_VALIDATORS variable to the standard set 
here: 
https://docs.djangoproject.com/en/1.9/topics/auth/passwords/#enabling-password-validation.
 
When I use the `createsuperuser` function in manage.py, my password must 
conform to the validators. But when I use User.objects.create_user() or the 
user.set_password() methods, the AUTH_PASSWORD_VALIDATORS is not used. I 
can literally set my password to 'a' and its accepted.

Is this a bug? It seems like the validators should be used when creating a 
new user or setting the password.
Is is there an alternative method to validate the password prior to 
creation?

I am using Django 1.9.7

Thanks!
---
Farhan Khan
PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B

-- 
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/5d7e0472-5d1b-42a4-a640-b93b09023de3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: Contribute to Django - Python 3 or Python 2

2016-06-28 Thread Farhan Khan
I switched from Python2 to Python3 because I needed my Django code to fire 
off some TLSv1.2 code, that python2 could not do. I barely notice the 
difference between the two except in very small syntax differences. When I 
switched from python2 to python3, I literally did this:

find ./ -name "*.py" -exec 2to3 -w {} \;

And everything worked :)

I found it to have a more profound impact in other frameworks, such as 
Twisted or any type of socket programming, where you need to worry about 
encoding/decoding between unicode and binary strings.

On Tuesday, June 28, 2016 at 7:45:02 PM UTC-4, premdjango wrote:
>
> Hello,
> Im trying to contribute to Django project and started with this document.
>
> https://docs.djangoproject.com/en/dev/intro/contributing/
>
> Here in couple of places I see Python3 is being used but in most places 
> python2 is used.
>
> Should I use Python 3 or Python 2?
>
>
>

-- 
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/9ca05994-67cc-4ee4-bddd-1bd912dbe406%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: AUTH_PASSWORD_VALIDATORS not used when creating new users?

2016-06-28 Thread Farhan Khan
Even when I use User.objects.create_user() I am still not required to use a 
strong password. Considering that createsuperuser and changepassword are 
management commands, it seems odd that require a password that conforms 
with AUTH_PASSWORD_VALIDATORS there, but not with a function I would use in 
production code.

On Tuesday, June 28, 2016 at 10:00:06 PM UTC-4, Tim Graham wrote:
>
> Validation happens in the management commands (createsuperuser, 
> changepassword) as well as the user creation forms. We assume that if 
> you're creating a user using the command line, you know what you're doing. 
> If you want to open a documentation ticket, we could add a note about this.
>
> On Monday, June 27, 2016 at 9:29:56 PM UTC-4, Farhan Khan wrote:
>>
>> Hi all,
>>
>> I set the AUTH_PASSWORD_VALIDATORS variable to the standard set here: 
>> https://docs.djangoproject.com/en/1.9/topics/auth/passwords/#enabling-password-validation.
>>  
>> When I use the `createsuperuser` function in manage.py, my password must 
>> conform to the validators. But when I use User.objects.create_user() or the 
>> user.set_password() methods, the AUTH_PASSWORD_VALIDATORS is not used. I 
>> can literally set my password to 'a' and its accepted.
>>
>> Is this a bug? It seems like the validators should be used when creating 
>> a new user or setting the password.
>> Is is there an alternative method to validate the password prior to 
>> creation?
>>
>> I am using Django 1.9.7
>>
>> Thanks!
>> ---
>> Farhan Khan
>> PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B
>>
>

-- 
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/e44a9871-0aeb-4cc4-96ec-a9866e6d6f74%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: AUTH_PASSWORD_VALIDATORS not used when creating new users?

2016-07-02 Thread Farhan Khan
So then, how are password validators used? If its just through the console 
using the 'createsuperuser' feature, what good is it?

On Friday, July 1, 2016 at 7:44:56 PM UTC-4, Tim Graham wrote:
>
> Password validation doesn't run as part of Model.clean(), but Model.save() 
> doesn't run validation (Model.clean()) anyway.
>
> If you're interested, here's the password validation design decisions 
> thread on django-developers: 
> https://groups.google.com/d/topic/django-developers/9GBhgGXmEKs/discussion
>
> On Tuesday, June 28, 2016 at 10:33:13 PM UTC-4, Farhan Khan wrote:
>>
>> Even when I use User.objects.create_user() I am still not required to use 
>> a strong password. Considering that createsuperuser and changepassword are 
>> management commands, it seems odd that require a password that conforms 
>> with AUTH_PASSWORD_VALIDATORS there, but not with a function I would use in 
>> production code.
>>
>> On Tuesday, June 28, 2016 at 10:00:06 PM UTC-4, Tim Graham wrote:
>>>
>>> Validation happens in the management commands (createsuperuser, 
>>> changepassword) as well as the user creation forms. We assume that if 
>>> you're creating a user using the command line, you know what you're doing. 
>>> If you want to open a documentation ticket, we could add a note about this.
>>>
>>> On Monday, June 27, 2016 at 9:29:56 PM UTC-4, Farhan Khan wrote:
>>>>
>>>> Hi all,
>>>>
>>>> I set the AUTH_PASSWORD_VALIDATORS variable to the standard set here: 
>>>> https://docs.djangoproject.com/en/1.9/topics/auth/passwords/#enabling-password-validation.
>>>>  
>>>> When I use the `createsuperuser` function in manage.py, my password must 
>>>> conform to the validators. But when I use User.objects.create_user() or 
>>>> the 
>>>> user.set_password() methods, the AUTH_PASSWORD_VALIDATORS is not used. 
>>>> I can literally set my password to 'a' and its accepted.
>>>>
>>>> Is this a bug? It seems like the validators should be used when 
>>>> creating a new user or setting the password.
>>>> Is is there an alternative method to validate the password prior to 
>>>> creation?
>>>>
>>>> I am using Django 1.9.7
>>>>
>>>> Thanks!
>>>> ---
>>>> Farhan Khan
>>>> PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B
>>>>
>>>

-- 
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/6fe2faeb-d7e0-431d-836e-8a715bdc9a7b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


ManyToManyField('self') not using related_name

2016-07-19 Thread Farhan Khan
I am using a ManyToManyField whose target is a 'self' reference, but it 
does not create a related_name "reverse" field.

Here is my model:

class SecurityGroup(models.Model):
name = models.CharField(max_length=100) 
description = models.TextField() 
subgroups = models.ManyToManyField('self', related_name='origin') 

def __str__(self): 

return self.name


And here is the problem:


>>> x = SecurityGroup(name='name', description='description')
>>> x.save()
>>> x.origin()
Traceback (most recent call last):
  File "", line 1, in 
AttributeError: 'SecurityGroup' object has no attribute 'origin'


There also no x.securitygroup_set. Is this a bug?



-- 
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/9a8d0c8c-9429-4340-be69-78b8e6a5a296%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: ManyToManyField('self') not using related_name

2016-07-19 Thread Farhan Khan
The solution was that I needed to set symmetrical=False, per the 
documentation 
here: 
https://docs.djangoproject.com/en/1.9/ref/models/fields/#django.db.models.ManyToManyField.symmetrical

On Tuesday, July 19, 2016 at 2:52:16 PM UTC-4, Farhan Khan wrote:
>
> I am using a ManyToManyField whose target is a 'self' reference, but it 
> does not create a related_name "reverse" field.
>
> Here is my model:
>
> class SecurityGroup(models.Model):
> name = models.CharField(max_length=100) 
> description = models.TextField() 
> subgroups = models.ManyToManyField('self', related_name='origin') 
>
> def __str__(self): 
>
> return self.name
>
>
> And here is the problem:
>
>
> >>> x = SecurityGroup(name='name', description='description')
> >>> x.save()
> >>> x.origin()
> Traceback (most recent call last):
>   File "", line 1, in 
> AttributeError: 'SecurityGroup' object has no attribute 'origin'
>
>
> There also no x.securitygroup_set. Is this a bug?
>
>
>
>

-- 
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/8076eb42-3604-43ad-aec2-94d04d0c6602%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


MultiWidget not rendering

2016-12-19 Thread Farhan Khan
Hi all!
I am trying to get a MultiWidget with two TextFields ,however I am not able 
to get anything to render. Here is my code.

from django import forms
from django.forms import widgets

class DateSelectorWidget(widgets.MultiWidget):
def __init__(self, attrs=None):
_widgets = (forms.TextInput(),
forms.TextInput())
super(DateSelectorWidget, self).__init__(_widgets, attrs)

def format_output(self, rendered_widgets):
return ''.join(rendered_widgets)

class Fourteen(forms.Form):
mywidget = DateSelectorWidget()



In the shell I will do:

>>> fourteen = Fourteen()
>>> print(fourteen)

>>>

Nothing will render. I suspect that I need to manually render the HTML, but 
the documentation 
 is 
fairly light on this topic.

Any assistance would be greatly appreciated!

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/86bebdb4-1d45-4612-84d1-456f86942709%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: MultiWidget not rendering

2016-12-19 Thread Farhan Khan
Unfortunately that does not display.
In the template I had it as {{ fourteen }} and saw it was rendering
everything but my custom widget.

--
Farhan Khan
PGP Fingerprint: 4A78 F071 5CB6 E771 B8D6 3910 F371 FE22 3B20 B21B

On Mon, Dec 19, 2016 at 2:02 PM, Matthew Pava  wrote:

> I’m not that familiar with MultiWidget, but it would seem that your print
> statement should be:
>
> print(fourteen.mywidget)
>
>
>
> *From:* django-users@googlegroups.com [mailto:django-users@
> googlegroups.com] *On Behalf Of *Farhan Khan
> *Sent:* Monday, December 19, 2016 11:40 AM
> *To:* Django users
> *Subject:* MultiWidget not rendering
>
>
>
> Hi all!
>
> I am trying to get a MultiWidget with two TextFields ,however I am not
> able to get anything to render. Here is my code.
>
>
>
> from django import forms
> from django.forms import widgets
>
> class DateSelectorWidget(widgets.MultiWidget):
> def __init__(self, attrs=None):
> _widgets = (forms.TextInput(),
> forms.TextInput())
> super(DateSelectorWidget, self).__init__(_widgets, attrs)
>
> def format_output(self, rendered_widgets):
> return ''.join(rendered_widgets)
>
> class Fourteen(forms.Form):
> mywidget = DateSelectorWidget()
>
>
>
> In the shell I will do:
>
>
>
> >>> fourteen = Fourteen()
>
> >>> print(fourteen)
>
>
>
> >>>
>
>
> Nothing will render. I suspect that I need to manually render the HTML,
> but the documentation
> <https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#multiwidget>
> is fairly light on this topic.
>
>
>
> Any assistance would be greatly appreciated!
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/86bebdb4-1d45-4612-84d1-456f86942709%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/86bebdb4-1d45-4612-84d1-456f86942709%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "Django users" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/django-users/hUmvw-mdaiY/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at https://groups.google.com/group/django-users.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/django-users/ebd9f58fca3a4009abcfd05e16fca9dc%40ISS1.ISS.LOCAL
> <https://groups.google.com/d/msgid/django-users/ebd9f58fca3a4009abcfd05e16fca9dc%40ISS1.ISS.LOCAL?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/CAFd4kYAkQ3jNf-Hbvt0bz3xB%3DskxKBEPcnrqNcOhPObniZ%2BS4g%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


Re: MultiWidget not rendering

2016-12-25 Thread Farhan Khan
Hi all, I figured out how it works.

Using MultiWidget requires three components: MultiWidget, MultiValueField 
and a form.

Here is my example:

class ComplexMultiWidget(forms.MultiWidget):
def __init__(self, attrs=None):
widgets = (
forms.DateInput(),
forms.Select(choices=(('D', 'Days'),
  ('W', 'Weeks'),
  ('Y', 'Years'))),
)
super(ComplexMultiWidget, self).__init__(widgets, attrs)

def decompress(self, value):
if value:
data = value.split(',')
return [data[0], data[1]]
return [None, None, None]

def format_output(self, rendered_widgets):
return u'\n'.join(rendered_widgets)


class ComplexField(forms.MultiValueField):
def __init__(self, required=True, widget=None, label=None, initial=None):
fields = (
forms.DateField(widget=forms.DateInput),
forms.ChoiceField(choices=(('D', 'Days'),
   ('W', 'Weeks'),
   ('Y', 'Years'))),
)
super(ComplexField, self).__init__(fields, required,
   widget, label, initial)

def compress(self, data_list):
if data_list:
return '%s,%s' % (data_list[0],''.join(data_list[1]))
return None

class ComplexFieldForm(forms.Form):
field1 = ComplexField(widget=ComplexMultiWidget())


I took the solution from here: http://stackoverflow.com/a/2387330

I hope this help someone in the future!



On Monday, December 19, 2016 at 12:40:12 PM UTC-5, Farhan Khan wrote:
>
> Hi all!
> I am trying to get a MultiWidget with two TextFields ,however I am not 
> able to get anything to render. Here is my code.
>
> from django import forms
> from django.forms import widgets
>
> class DateSelectorWidget(widgets.MultiWidget):
> def __init__(self, attrs=None):
> _widgets = (forms.TextInput(),
> forms.TextInput())
> super(DateSelectorWidget, self).__init__(_widgets, attrs)
>
> def format_output(self, rendered_widgets):
> return ''.join(rendered_widgets)
>
> class Fourteen(forms.Form):
> mywidget = DateSelectorWidget()
>
>
>
> In the shell I will do:
>
> >>> fourteen = Fourteen()
> >>> print(fourteen)
>
> >>>
>
> Nothing will render. I suspect that I need to manually render the HTML, 
> but the documentation 
> <https://docs.djangoproject.com/en/1.10/ref/forms/widgets/#multiwidget> 
> is fairly light on this topic.
>
> Any assistance would be greatly appreciated!
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/548457cb-eeed-4cca-830c-cd7847b9f665%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: What determines initial number of forms in Django modelformset_factory?

2016-12-28 Thread Farhan Khan
I believe its the "extra" parameter.
By default, the size is the number of objects you pass it. You can set that 
value to 0 by doing YourObject.objects.none()

On Wednesday, December 28, 2016 at 7:17:31 AM UTC-5, Duane Gregory wrote:
>
> My model formset is producing an initial 7 forms, as seen here:
>
>  value="7" /> type="hidden" value="7" />
>
> I would like to alter that number, but cannot see how.
> view and form for ref:
>
> def micro_log_create(request):
> MicroLogFormSet = modelformset_factory(Micro_Log, form=MicroLogForm, 
> max_num=4, extra=0)
> if request.method == 'POST':
> formset = MicroLogFormSet(request.POST, request.FILES)
> if formset.is_valid():
> formset.save()
> return reverse('micro-log')
> else:
> formset = MicroLogFormSet()
> return render(request, 'app/micro_log/micro_log_create.html', 
> {'formset': formset})
>
> class MicroLogForm(forms.ModelForm):
> class Meta:
> model = Micro_Log   
> exclude = ['user', 'date_time']
> MicroLogFormSet = modelformset_factory(Micro_Log, form=MicroLogForm, 
> max_num=4, extra=0)
> formset = MicroLogFormSet()
>
> 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/8e444739-f6d4-4814-a678-dcc27663a00d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Custom Widget for CharField Multiple

2016-12-28 Thread Farhan Khan
Hi,
I am trying to create a `CharFieldMultiple`, equivalent to the 
MultipleHiddenInput or SelectMultiple.

The goal is to have multiple CharField returned as a list, but have not 
been able to recreate it. My code thus far is:

class CharFieldMultiple(forms.widgets.Input):
def render(self, name, value, attrs=None):
if value is None:
value = []
final_attrs = self.build_attrs(attrs, type=self.input_type, name=
name)
inputs = []
for i, v in enumerate(value):
input_attrs = dict(value=force_text(v), **final_attrs)
inputs.append(format_html('', flatatt(input_attrs)))
return mark_safe('\n'.join(inputs))

I am trying to figure out how 'value' is set. I noticed that when I 
manually put value = ['a','b','c'], I will get 3 CharFields, great! But I 
cannot figure out how to pass that value parameter.

The type-field is still set to None, but I can figure that out later.
Thanks in advance!

- Farhan

-- 
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/2ecc728a-8d13-492d-91cf-e2fe155fc04b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.