How to create a built-in foreign key feedback formset

I have a property model as follows =

class Property(models.Model):
    property_type = models.CharField(max_length=255, default='Apartment')
    specifications = models.CharField(max_length=255, default='Basic')
    built_up_area = models.FloatField(max_length=6, null=False, default=0)
    total_area = models.FloatField(null=False, default=0)
    number_of_bedrooms = models.CharField(max_length=3, default=1)
    number_of_bathrooms = models.CharField(max_length=3, default=1)
    number_of_parking_spaces = models.CharField(max_length=2, default=0)
    address_line_one = models.CharField(max_length=255, null=False)
    address_line_two = models.CharField(max_length=255, null=True, default=None)
    connectivity = models.CharField(max_length=255, default=None, null=True)
    neighborhood_quality = models.CharField(max_length=255, default=None,
                                            null=True)
    comments = models.CharField(max_length=255, default=None, null=True)
    city = models.ForeignKey('City')
    state = models.ForeignKey('State')
    pin_code = models.ForeignKey('PinCode')
    developer = models.ForeignKey('Developer', null=True, default=None)
    owner = models.ForeignKey('Owner', null=True, default=None)
    created_by = models.ForeignKey('custom_user.User')

    project = models.ForeignKey('Project')

    def __unicode__(self):
        return self.property_type

    class Meta:
        verbose_name_plural = 'Properties'

      

And the city model is as follows -

class City(models.Model):
    name = models.CharField(max_length=255)
    slug = models.SlugField(unique=True)

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(City, self).save(*args, **kwargs)

    def __unicode__(self):
        return self.name

      

Now I want to create a single form where I can enter property details and at the time of entering the city, I can enter the name of the city instead of choosing from the dropdown list.

So how do I create an inline formset using inlineformset_factory to create the form?

== == EDIT

I tried using the following code to create a form set

CityFormset = inlineformset_factory(City, Property,
                                       fields=('city',),
                                       extra=0,
                                       min_num=1,
                                       can_delete=False)

      

+1


source to share


1 answer


You misunderstood what an inline formset is. This is for editing the "many" side of a one-to-many relationship, that is, given the parent city model, you can edit the built-in various properties that belong to that city.

You don't want the formset to edit at all the only city the property can belong to. Instead, override the field city

in your properties form as TextField and create a new city or find an existing method clean_city

.



class PropertyForm(forms.ModelForm):
    city = forms.TextField(required=True)

    class Meta:
        model = Property
        exclude = ('city',)

    def __init__(self, *args, **kwargs):
        super(PropertyForm, self).__init__(*args, **kwargs)
        if self.instance and not self.data:
            self.initial['city'] = self.instance.city.name

    def save(self, commit=True):
        city_name = self.cleaned_data['city']
        city, _ = City.objects.get_or_create(name=city_name)
        instance = self.save(commit=False)
        instance.city = city
        if commit = True:
            instance.save()
        return instance

      

+3


source







All Articles