0

This may be easy, but I have not been able to solve the issue I have 2 models wwith oneToMany relation. I am try to update the child table using foreign key of parent table, but it is returning this error:

ValidationError at /StudentParentsAssign/01b93b30-e5fb-49f8-9394-94a824b7a099/ ['“c0ea3f3e-bb79-4fc5-9fd9-6bce341324d1,” is not a valid UUID.']

enter image description here

enter image description here

Below is my code:

Models.py

#Modeles.py
class Student(models.Model):
    #std_matricule = models.CharField(verbose_name='Student matricule', max_length=6, null=False, unique=True, primary_key=True)
    std_matricule = models.CharField(verbose_name='Matricule', unique=True, max_length=16, null=False, blank=False, help_text='Matricule of the student')
    std_parents = models.ForeignKey(Parents, on_delete=models.DO_NOTHING, related_name='Parents', unique=False, null=True, blank=True, verbose_name='Student parents')
    

class Parents(models.Model):
    father_surname = models.CharField(verbose_name='Father surname', max_length=128, null=False, blank=True, help_text='Student Father surname as in the birth certificate')
    father_firstName = models.CharField(verbose_name='Father name', max_length=128, null=False, blank=True)
    parent_id = models.UUIDField(default=uuid.uuid4, unique=True, primary_key=True, editable=False)
    ....

view.py

#view.py
def StudentParentsAssign(request, pk):
    # parents_list = Parents.objects.all().order_by('father_surname')
    std = Student.objects.get(student_id = pk)
    if std.std_parents not in (None, ''):
        #  parentsList = Parents.objects.all().order_by('father_surname')
         context = {'studentObj': std}
         return render(request, "students_management_app/student_template/student-single.html", context)
    else:
        if request.method == "POST":
            perents_id = request.POST.get('radio')
            Student.objects.filter(pk=pk).update(std_parents=perents_id)
        parentList = Parents.objects.filter(parents_deleteStatus=False).order_by('-parent_createDate')
        context = {'parentList': parentList}
    return render(request, "students_management_app/student_template/student-parents-list.html", context)

url.py

#url.py 
path('studentDetails/<uuid:pk>/', views.studentDetails, name = 'student-details'),
path('StudentParentsAssign/<uuid:pk>/', views.StudentParentsAssign, name = 'student-parents-list'),

Template

#Template
{% block content %}   
    <h1>List of all parents</h1>
    {% if parentList %}
    <form action="", method="POST">
        {% csrf_token %}
    <button type="submit">Assign the selected parents</button>
    <table>
        <tr>
            <th>Select</th>
            <th>Father's full name</th>
            <th>Father's phone</th>
            <th>Mother's full name </th>
            <th>Mother's phone</th>
            <th>View</th>
            <th>Update</th>
        </tr>
        {% for parent in parentList %}
        <tr>
            <td>
                <input type="radio", value={{parent.parent_id}}, name="radio"/>
            </td>
            <td>{{parent.father_surname}} {{parent.father_firstName}}</td>
            <td>{{parent.father_phone}}</td>
            <td>{{parent.mother_firstName}} {{parent.mother_surname}} </td>
            <td>{{parent.mother_phone}}</td>
            <td><a href="{% url 'parents-details' parent.parent_id %}"> View</a></td>
            <td><a href="{% url 'parents-update' parent.parent_id %}"> Update </a></td>
            <td><a href="#"> Update </a></td>
            <td><a href="#"> Delete </a></td>
        </tr>
        {% endfor %}
    </table>
</form>
    {% else %}
    <p>No parent found in the database</p>
    {%endif%}
{% endblock content %}

Thank you

2
  • Could you please share the full traceback ?
    – Alombaros
    Mar 19 at 9:11
  • I just add the traceback, inserted it as an image. Thank you Alombaros
    – Eric
    Mar 19 at 11:36

2 Answers 2

0

I'm not sure how UUID FK works but I guess something like that should_work

perents_id = request.POST.get('radio')
parent = Parents.objects.get(parent_id=perents_id)
Student.objects.filter(pk=pk).update(std_parents=parent)

Usually you'll be able to update with:

perents_id = request.POST.get('radio')
Student.objects.filter(pk=pk).update(std_parents_id=perents_id)

But I don't know if this applies to UUID FK

Note 1: Model name should not be plurialized tip
Note 2: using .update() to update a single object might be detrimental because it bypasses the .save() doc

3
  • Thanks @Alomboros, I just modified it as per your suggestions, but it is not working. I even try this and I am having the same error. ` parents_id = request.POST.get('radio') parent = Parents.objects.get(parent_id=parents_id) # Student.objects.filter(pk=pk).update(std_parents=parent) std = Student.objects.filter(pk=pk) std = std.std_parents=parent std.save() ` My application requirement is to use uuid for PK. Thank you
    – Eric
    Mar 19 at 13:00
  • I just spotted it in you question, I don't “c0ea3f3e-bb79-4fc5-9fd9-6bce341324d1,” should end with a comma.
    – Alombaros
    Mar 19 at 13:04
  • I tried to remove the comma at the end of the uuid, but seems not working. I even tried to convert the string to uuid. I add this code parent.replace(',', '') par = uuid.UUID(parent).hex Student.objects.filter(pk=pk).update(std_parents=par) code
    – Eric
    Mar 19 at 15:16
0

I just found that an additional character was added at the end of the uuid depending on the method I used. I used replace() function to remove and this is the view.py

def StudentParentsAssign(request, pk):
std = Student.objects.get(student_id = pk)
if std.std_parents not in (None, ''):
     context = {'studentObj': std}
     return render(request, "students_management_app/student_template/student-single.html", context)
else:
    if request.method == "POST":
        par_id = request.POST.get('radio') #['radio']
        par = par_id.replace('/', '')
        parent = Parents.objects.get(parent_id=par)
        Student.objects.filter(pk=pk).update(std_parents=parent)
        context = {'studentObj': std}
        return render(request, "students_management_app/student_template/student-single.html", context)
    parentList = Parents.objects.filter(parents_deleteStatus=False).order_by('-parent_createDate')
    context = {'parentList': parentList}
return render(request, "students_management_app/student_template/student-parents-list.html", context)

Thank @Alombaros

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.