-1

I'm successfully initialising the values of the field Ip using a custom inline FormSet:

# http://stackoverflow.com/a/24462173/129805
class PrepopulatedIpInlineFormSet(forms.models.BaseInlineFormSet):
    model = Ip

    def __init__(self, *args, **kwargs):
        super(PrepopulatedIpInlineFormSet, self).__init__(*args, **kwargs)
        apn_ggsns = ApnGgsn.objects.all()
        initial = []
        for apn_ggsn in apn_ggsns:
            print apn_ggsn.cidr
            cidr = IPNetwork(apn_ggsn.cidr)
            print cidr
            for ip in cidr:
                # skip .0 and .255 addresses
                ipstr = str(ip)
                print ipstr[-2:]
                if ipstr[-2:] == '.0' or ipstr[-4:] == '.255':
                    continue
                print ipstr
                num = Ip.objects.filter(ip=ipstr).count()#, apnid=apn_ggsn.apn_id, ggsn_id=apn_ggsn.ggsn_id).count()
                if num == 0:
                    initial.append({'ip': ipstr, 'apn': apn_ggsn.apn, 'ggsn': apn_ggsn.ggsn})
                    break
        self.initial = initial

The ip field is being correctly populated on the admin form with the next free ip address. Multiple inline forms/objects are being generated correctly.

I cannot get the <select>s to show the correct initial values.

I have tried each combination of adding a _id suffix to the dictionary key and using the actual object and the object's id as the value.

The <select> boxes never show an initial selected value.

The documentation shows examples, but not of ForeignKeyFields.

https://docs.djangoproject.com/en/1.9/topics/forms/formsets/#using-initial-data-with-a-formset

What am I doing wrong?

fadedbee
  • 42,671
  • 44
  • 178
  • 308

1 Answers1

0

It was a browser refresh bug. When I opened the page a new tab, it all worked as expected.

fadedbee
  • 42,671
  • 44
  • 178
  • 308