im having an issue here and cant find out whats happening.
I have and DRF REST API which must receive a POST to create an AlertRule object and a couple of EmailAddress objects related to it. The problem is that when debugging the validator method, the email data should come on the attr param on the validator mathod, but its just not there when i send the data obver http post.
Example of what im sending:
{'alert_event_max_count': 2, 'alert_time_window': 2, 'company': 'Q29tcGFueToxNw==', 'company_id': 17, 'comparison_rule': 4, 'comparison_value': '0', 'daily_start_time': '07:00:00', 'daily_stop_time': '17:59:59', 'emails': [{'email':'bruno.carli@smartgreen.net'}], 'end_date': '2022-01-01', 'installation_ids': [], 'monitored_property_id': 30, 'name': 'kk', 'start_date': '2019-01-01', 'week_day_bitmask':17}
The attr only shows:
OrderedDict([('alert_time_window', 2), ('alert_event_max_count', 2), ('company', <Company: Company object (17)>), ('comparison_rule', 4), ('comparison_value', '0'), ('daily_start_time', datetime.time(7, 0)), ('daily_stop_time', datetime.time(17, 59, 59)), ('end_date', datetime.date(2022, 1, 1)), ('installations', []), ('monitored_property', <Property: None - None>), ('name', 'loli'), ('start_date', datetime.date(2019, 1, 1)), ('week_day_bitmask', 17)])
Where is the 'emails': [{'email':'bruno.carli@smartgreen.net'}], data i have sent?
Here the code:
serializers
class EmailSerializer(serializers.ModelSerializer):
class Meta:
fields = ['id', 'email']
model = EmailAddress
class AlertRuleSerializer(serializers.ModelSerializer):
emails = EmailSerializer(many=True, required=False)
... # other fields
class Meta:
model = AlertRule
fields = [
... # other fields
'emails'
]
depth = 2
def validate(self, attrs): # pylint: disable=arguments-differ
"""
Valida se as instalações recebidas são da mesma empresa da regra
de alerta.
"""
installations = attrs.get('installations', [])
company = attrs.get('company')
print(attrs) # show received attributes data
return attrs
models:
class EmailAddress(models.Model):
alert_rule = models.ForeignKey(
'AlertRule',
related_name='emails',
on_delete=models.CASCADE
)
email = models.EmailField(unique=True)
At the end, the AlertRule is created, but withou creating any email object :/
'{"alert_time_window":2,"alert_event_max_count":2,"company":{"id":17,"name":"local3","cnpj":"93293944000185"},"comparison_rule":4,"comparison_value":"0","daily_start_time":"07:00:00","daily_stop_time":"17:59:59","end_date":"2022-01-01","id":25,"installations":[],"monitored_property":{"id":30,"measurement_unit":null,"name":null,"source_name":"internal_temperature","service":"","short_name":null},"name":"lol","start_date":"2019-01-01","week_day_bitmask":17,"emails":[]}'
Any tip?
Aucun commentaire:
Enregistrer un commentaire