I'm making a webapp with django, and one of my models needs to perform validation to ensure that one of its relational properties follows specific guidelines. I've been working with ChadGPT to accomplish all this, and he gave me 2 methods for this validation: a custom save method for the model, and using clean method from django. Is there a best practice method for this, or are both ways fine to use?
#🔒 Django model validation
8 messages · Page 1 of 1 (latest)
@mystic creek
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
class SystemInputOutput(models.Model):
system = models.ForeignKey(System, on_delete=models.CASCADE, related_name='input_output_relations')
related_system = models.ForeignKey(System, on_delete=models.CASCADE, related_name='+')
related_system_state = models.ForeignKey(SystemState, on_delete=models.CASCADE, related_name='+')
is_input = models.BooleanField(default=True) # True for input, False for output
def __str__(self):
return f"{'Input' if self.is_input else 'Output'}: {self.system.name} -> {self.related_system.name} ({self.related_system_state.state_name})"
class SystemState(models.Model):
system = models.ForeignKey(System, on_delete=models.CASCADE, related_name='states')
state_name = models.CharField(max_length=255)
def __str__(self):
return f"{self.system.name} - {self.state_name}"
class System(models.Model):
name = models.CharField(max_length=255)
parent_system = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='subsystems')
def __str__(self):
return self.name
Here are the models. I want to validate the related_system_state to make sure that it's a valid system_state for related_system
ChatGPT isn't guaranteed to provide reliable code.
I really don't need to hear this every time I open a help ticket. GPT4 is more than capable of coding full enterprise level apps with the proper guidance. The validity of the code is not in question here, I'm just trying to get some advice on best practices.
¯_(ツ)_/¯
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.