0

Initial Error : When CustomerID was CharField in models.py :

Error Message :

after making changes to the model and making the CustomerID UUIDField getting the below error :

enter image description here

I am trying to redirect to the view based on the CustomerID but its not working for UUIDs. It was previously working fine with integers.

Please suggest.

url.py:

from django.urls import path, include
from . import views

urlpatterns = [
   path('create/', views.create, name='create'),
   path('<uuid:customer_id>', views.detail, name='detail'),
   path('search/customers/<uuid:customer_id>', views.detail, name='detail'),
   path('customers/<uuid:customer_id>', views.detail, name='detail'),
   path('edit/<uuid:customer_id>', views.edit, name='edit'),
   path('modify/<uuid:customer_id>', views.modify, name='modify'),
]

views.py

 @login_required
 def detail(request, customer_id):
     customer = get_object_or_404(CustomerMaster, pk=customer_id)
     return render(request, 'customers/detail.html',{'customer':customer})

models.py

   class CustomerMaster(models.Model):
         customerid = models.UUIDField(db_column='CustomerID', primary_key=True)  # Field name made lowercase.
         customernumber = models.CharField(db_column='CustomerNumber', max_length=50)  # Field name made lowercase.
         customername = models.CharField(db_column='CustomerName', max_length=50)  # Field name made lowercase.
          lastmodifiedutc = models.DateTimeField(db_column='LastModifiedUTC')  # Field name made lowercase.
          lastmodifiedby = models.CharField(db_column='LastModifiedBy', max_length=50)  # Field name made lowercase.
          active = models.BooleanField(db_column='Active')  # Field name made lowercase.
         customershortname = models.CharField(db_column='CustomerShortName', max_length=50, blank=True, null=True)  # Field name made lowercase.


      class Meta:
            managed = False
            db_table = 'Customer_Master'

            def __str__(self):
                return self.CustomerName
Kuntal
  • 39
  • 6

3 Answers3

0

The letters in the UUID must be lowercase according to the docs:

uuid - Matches a formatted UUID. To prevent multiple URLs from mapping to the same page, dashes must be included and letters must be lowercase. For example, 075194d3-6885-417e-a8a8-6c931e272f00. Returns a UUID instance.

Selcuk
  • 45,843
  • 11
  • 87
  • 90
  • entries in the database are in CAPS.. not sure how to handle lower case UUIDs in that case. – Kuntal Sep 24 '19 at 05:45
  • You are using a UUIDField, which handles transformation of UUID to/from the db representation. In other words, how you see the value in the db is not necessarily how the value is handled once in django land. – monkut Sep 24 '19 at 05:48
  • @monkut - thanks for your input. In that case, how do i transform lowercase to Uppercase in the view.py detail method when doing a get ? Please see my error screenshots above. – Kuntal Sep 24 '19 at 05:52
  • Please don't add new questions by editing your original one. Your initial issue was the regex mismatch. I suggest you to post the new issue as another question. – Selcuk Sep 24 '19 at 05:56
  • this is not a new question... I did the modification and still stuck on the same issue "Django urls with UUID issue" – Kuntal Sep 24 '19 at 06:02
0

For the UUIDField looks like you are using Microsoft SQLServer database that does not support the UUIDField properly....

So if you use CharField you probably want to introduce a regex UUID check on the incoming uuid field checkout this:

Searching for UUIDs in text with regex

As mentioned you want to make sure that the DB value is consistent so using .lower() on the validated input would be a good idea.

monkut
  • 36,357
  • 21
  • 109
  • 140
-1

Resolved by making the CustomerID as CharField and passing string parameter in the URL.

Kuntal
  • 39
  • 6
  • don't post an answer with "solved it via ..." but rather mark an answer as accepted if it helped you. – wfehr Sep 24 '19 at 07:14