6

I have a form inheriting from ModelForm as such:

class ChildModel(ModelForm):
      class Meta:
          model = Documents
          fields = ('secretdocs')
          widgets = {
              'secretdocs': Select(attrs={'class': 'select'}),
          }

The model "secretdocs" has a uid. But when it prints out the select and option, the option values appear as such:

<select class="select" id="id_secretdocs" name="secretdocs">
    <option value="1">My Secret Doc</option>
</select>

But I want it to instead have the uid of the option:

<select class="select" id="id_secretdocs" name="secretdocs">
    <option value="cd2feb4a-58cc-49e7-b46e-e2702c8558fd">My Secret Doc</option>
</select>

I've so far tried to use BaseForm's data object and overwriting Select's value_from_datadict method but I'm pretty sure that wasn't the right approach. Does anyone know how I can do this?

Thanks in advance.

limasxgoesto0
  • 4,555
  • 8
  • 31
  • 38

2 Answers2

17

You can do something like this:

class ChildModel(ModelForm):

  secretdocs = forms.ChoiceField(choices=[(doc.uid, doc.name) for doc in Document.objects.all()])
  class Meta:
      model = Documents
      fields = ('secretdocs', )
      widgets = {
          'secretdocs': Select(attrs={'class': 'select'}),
      }
karthikr
  • 97,368
  • 26
  • 197
  • 188
  • While this did work in that the uids show, it doesn't seem like I'm not able to filter my queryset using the same query that worked previously. In all fairness, I'm pretty new to Django so maybe there's something extra I need to add? – limasxgoesto0 Apr 12 '13 at 17:18
  • Yes - just replace `Document.objects.all()` with your previous filter criteria – karthikr Apr 12 '13 at 17:29
  • It needs to be filtered by the current user. I assume this can be done using lambdas, correct? I don't have time to try that now but I'll look into it soon. – limasxgoesto0 Apr 12 '13 at 18:54
  • wel.. you can do that easily in the `__init__` look into how to get the request object into init context and load the choices there. – karthikr Apr 12 '13 at 19:00
  • Found this link and it helped me with this process: http://stackoverflow.com/questions/3010489/how-do-i-filter-values-in-a-django-form-using-modelform. Not 100% done with this yet but I've made some good progress. – limasxgoesto0 Apr 14 '13 at 19:36
2

I think part of the "right" way to do this is to ensure that the UUID field is set as the primary key of the model in question and that there is no autonumber ID field.

For example:

class Document(models.Model):
    uuid = models.YourCustomUUIDField(primary_key=True,.....)
    other_field = models.CharField(.....)
    ....

If you do it this way, I think Django will pick up the UUID field as a primary key and use it in all places. If, however, you do wish to keep the built-in autoincrementing primary key field and use the UUID only here, you could do something similar to what karthikr suggests in those forms where the UUID field is required.

ZachS
  • 1,238
  • 7
  • 11