Models.py
class Palika(CreatedInfoModel):
name = models.CharField(
max_length=50,
help_text="Unique | Max: 50 characters | Gaun/Nagar Palika",
)
district = models.ForeignKey(
District, on_delete=models.PROTECT, help_text="Link to District"
)
is_default = models.BooleanField(default=False, help_text="By default=False")
active = models.BooleanField(default=True, help_text="by default=True")
class Ward(CreatedInfoModel):
name = models.CharField(
max_length=50,
# unique=True,
help_text="",
)
palika = models.ForeignKey(
Palika, on_delete=models.PROTECT, related_name="wards", help_text="link to Gaun/Nagar Palika"
)
active = models.BooleanField(default=True, help_text="by default=True")
def __str__(self):
return f"{self.name}"
serializers.py
class PalikaCreateSerializer(BaseModelSerializer):
wards_names = serializers.ListField(child=serializers.CharField())
class Meta:
model = Palika
# Write only fields
# 1. Common Write Only fields
common_write_only_fields = get_common_write_only_fields_create()
# 2. Custom Write Only fields with respect to model
custom_write_only_fields = [
"name",
"district",
"wards_names",
"is_default",
"active",
]
fields = common_write_only_fields + custom_write_only_fields
# Extra Kwargs
extra_kwargs = get_common_extra_kwargs_create()
extra_kwargs.update(
{
"name": {"write_only": True},
"district": {"write_only": True},
"wards_names": {"write_only": True},
"is_default": {"write_only": True},
"active": {"write_only": True},
}
)
def create(self, validated_data):
print(validated_data)
ward_names = validated_data.pop("wards_names")
print(validated_data)
validated_data["name"] = validated_data["name"].title()
palika = super().create(validated_data=validated_data)
for ward_name in ward_names:
Ward.objects.create(
app_type=palika.app_type,
device_type=palika.device_type,
created_date_ad=palika.created_date_ad,
created_by = palika.created_by,
name=ward_name,
palika=palika
)
return palika
def validate(self, attrs):
super().validate(attrs)
model_name = get_model_name_by_self_obj(self)
# Unique (Case Insensitive) validation
unique_keys_to_be_checked = []
unique_ci_char_create_validator = UniqueCICharCreateValidator(
CORE_APP_IMPORT_STRING, model_name, unique_keys_to_be_checked, attrs
)
unique_ci_char_create_validator(
CORE_APP_IMPORT_STRING, model_name, unique_keys_to_be_checked, attrs
)
# Default Check (Only 1 row with default value can exists)
default_keys_to_be_checked = ["is_default"]
single_instance_exists_create_validator = SingleInstanceExistsCreateValidator(
CORE_APP_IMPORT_STRING, model_name, default_keys_to_be_checked, attrs
)
single_instance_exists_create_validator(
CORE_APP_IMPORT_STRING, model_name, default_keys_to_be_checked, attrs
)
return super().validate(attrs)
I am getting error as below:
AttributeError: Got AttributeError when attempting to get a value for field wards_names
on serializer PalikaCreateSerializer
.
The serializer field might be named incorrectly and not match any attribute or key on the Palika
instance.
Original exception text was: 'Palika' object has no attribute 'wards_names'.