Python: How to solve the issue : 'badly formed hexadecimal UUID string' in Django -
i have created 'post' model in included 'post_id' primary key field. when trying create post, raising error: 'badly formed hexadecimal uuid string'. appreciate helping me in solve this.
here's code.
models.py:
class post(models.model): post_id = models.uuidfield(primary_key=true, default='uuid.uuid4', editable=false) user = models.foreignkey(settings.auth_user_model, default=1) from1 = models.charfield(max_length=20) = models.charfield(max_length=20) timestamp = models.datetimefield(auto_now=false, auto_now_add=true) objects = postmanager() def __unicode__(self): return self.post_id def __str__(self): return self.post_id def get_absolute_url(self): return reverse("posts:detail", kwargs={"post_id": self.post_id}) class meta: ordering = ["-timestamp", "-time"]
views.py:
def post_create(request): form = postform(request.post or none) if form.is_valid(): instance = form.save(commit=false) print(form.cleaned_data.get("post_id")) instance.user = request.user instance.save() return httpresponseredirect(instance.get_absolute_url()) context = { "form": form, } return render(request, "loggedin_load/post_load.html", context)
you need import module , not use quotes around 'uuid.uuid4'
.
it should like:
import uuid # uuid module class post(models.model): post_id = models.uuidfield(primary_key=true, default=uuid.uuid4, editable=false) # using function uuid4 on module user = models.foreignkey(settings.auth_user_model, default=1) from1 = models.charfield(max_length=20) = models.charfield(max_length=20) timestamp = models.datetimefield(auto_now=false, auto_now_add=true) objects = postmanager() def __unicode__(self): return self.post_id def __str__(self): return self.post_id def get_absolute_url(self): return reverse("posts:detail", kwargs={"post_id": self.post_id}) class meta: ordering = ["-timestamp", "-time"]
n.b i've not tested above code, , agree of comments shouldn't need uuid post_id. without knowing more couldn't further.
Comments
Post a Comment