Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - preventing duplicate records
    primarykey
    data
    text
    <p>I have a list of client records in my database. Every year, we generate a single work order for each client. Then, for each work order record, the user should be able to create a note that is specific to the work order. However, not all work orders need a note, just some.</p> <p>Now, I can't simply add a <code>note</code> field to the work order because some times, we need to create the note before the work order is even generated. Sometimes this note is specific to a work order that won't happen for 2-3 years. Thus, the notes and the work order must be independent, although they will "find" each other when they both exist. </p> <p>OK, so here's the situation. I want the user to be able to fill out a very simple <code>note</code> form, where they have two fields: <code>noteYear</code> and <code>note</code>. Thus, all they do is pick a year, and then write the note. The kicker is that the user should not be able to create two notes for the same year for the same client. </p> <p>What I'm trying to get as is validating the note by ensuring that there isn't already a note for that year for that client. I'm assuming this would be achieved by a custom <code>is_valid</code> method within the form, but I can't figure out how to go about doing that. </p> <p>This is what I tried so far (note that I know it's wrong, it doesn't work, but it's my attempt so far):</p> <p>Note that <code>systemID</code> is my client record</p> <p>My model:</p> <pre><code>class su_note(models.Model): YEAR_CHOICES = ( ('2013', 2013), ('2014', 2014), ('2015', 2015), ('2016', 2016), ('2017', 2017), ('2018', 2018), ('2019', 2019), ('2020', 2020), ('2021', 2021), ('2022', 2022), ('2023', 2023), ) noteYear = models.CharField(choices = YEAR_CHOICES, max_length = 4, verbose_name = 'Relevant Year') systemID = models.ForeignKey(System, verbose_name = 'System ID') note = models.TextField(verbose_name = "Note") def __unicode__(self): return u'%s | %s | %s' % (self.systemID.systemID, self.noteYear, self.noteType) </code></pre> <p>And my form:</p> <pre><code>class SU_Note_Form(ModelForm): class Meta: model = su_note fields = ('noteYear', 'noteType', 'note') def is_valid(self): valid = super (SU_Note_Form, self).is_valid() #If it is not valid, we're done -- send it back to the user to correct errors if not valid: return valid # now to check that there is only one record of SU for the system sysID = self.cleaned_data['systemID'] sysID = sysID.systemID snotes = su_note.objects.filter(noteYear = self.cleaned_data['noteYear']) for s in snotes: if s.systemID == self.systemID: self._errors['Validation_Error'] = 'There is already a startup note for this year' return False return True </code></pre> <p><strong>EDIT</strong> -- Here's my solution (thanks to janos for sending me in the right direction)</p> <p>My final form looks like this:</p> <pre><code>class SU_Note_Form(ModelForm): class Meta: model = su_note fields = ('systemID', 'noteYear', 'noteType', 'note') def clean(self): cleaned_data = super(SU_Note_Form, self).clean() sysID = cleaned_data['systemID'] sysID = sysID.systemID try: s = su_note.objects.get(noteYear = cleaned_data['noteYear'], systemID__systemID = sysID) print(s) self.errors['noteYear'] = "There is already a note for this year." except: pass return cleaned_data </code></pre> <p>For anyone else looking at this code, the only confusing part is the line that has: <code>sysID = sysID.systemID</code>. The <code>systemID</code> is actually a field of another model - even though <code>systemID</code> is also a field of this model -- poor design, probably, but it works. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload