Models as choices

31 views
Skip to first unread message

shreehari Vaasistha L

unread,
Apr 16, 2020, 4:52:07 AM4/16/20
to Django users
how can i use model x values as choices for model y ?

for eg:
class countries(models.Model):
 country
= models.CharField(max_length=200)

 def __str__(self):
 
return self.country

class User(AbstractUser):
 
"""User model."""

 username
= None
 full_name
= models.CharField(_("Full Name"), max_length=50, default="Full Name")
 country_choices
= models.CharField(choices=countries

Antje Kazimiers

unread,
Apr 16, 2020, 7:27:16 AM4/16/20
to django...@googlegroups.com

with a Foreign Key field, one-to-many relationship:

https://docs.djangoproject.com/en/3.0/topics/db/examples/many_to_one/

Antje

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/683251c6-27ab-4454-90d6-532fdcc749ce%40googlegroups.com.
Message has been deleted

shreehari Vaasistha L

unread,
Apr 16, 2020, 8:20:47 AM4/16/20
to Django users
Object of type ValueError is not JSON serializable
getting this above error.

here's my views.py
class UserCreateApiView(CreateAPIView):
   serializer_class = UserCreateSerializer
   def post(self,request):
       try:
           serializer=UserCreateSerializer(data=request.data)
           if serializer.is_valid():
               new_user = User()

                new_user.highest_degree=serializer.data['highest_degree']
               #new_user.state_registered=serializer.data['state_registered']
               new_user.country_choices=serializer.data['country_choices']

                new_user.save()
               print(new_user.doc_id)
               return Response(status=status.HTTP_200_OK)
           return Response(status=status.HTTP_406_NOT_ACCEPTABLE)
       except Exception as e:
           return Response(e)
have mentioned all fields in serializers.py




On Thursday, April 16, 2020 at 12:57:16 PM UTC+5:30, Antje Kazimiers wrote:

with a Foreign Key field, one-to-many relationship:

https://docs.djangoproject.com/en/3.0/topics/db/examples/many_to_one/

Antje

On 4/16/20 6:52 AM, shreehari Vaasistha L wrote:
how can i use model x values as choices for model y ?

for eg:
class countries(models.Model):
 country
= models.CharField(max_length=200)

 def __str__(self):
 
return self.country

class User(AbstractUser):
 
"""User model."""

 username
= None
 full_name
= models.CharField(_("Full Name"), max_length=50, default="Full Name")
 country_choices
= models.CharField(choices=countries
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.

Kasper Laudrup

unread,
Apr 16, 2020, 9:04:13 AM4/16/20
to django...@googlegroups.com
Hi Shreehari

On 16/04/2020 10.17, shreehari Vaasistha L wrote:
> Object of type ValueError is not JSON serializable
>
>
> getting this above error
>

First of all, don't highjack other unrelated threads. Create your own
with a descriptive subject.

You get the error because of the lines:

except Exception as e:
return Response(e)

You cannot serialize the exception object, but your real problem is most
likely the actual exception being thrown.

Try to remove the entire try/except block. You most likely don't want to
return errors in your code to the user anyway so returning an HTTP 500
to the user and letting Django write the exception message and
stacktrace in your log is probably much better.

Kind regards,

Kasper Laudrup

shreehari Vaasistha L

unread,
Apr 16, 2020, 9:14:26 AM4/16/20
to Django users
Thanks for helping me out .

Gavin Wiener

unread,
Apr 16, 2020, 12:01:07 PM4/16/20
to Django users
Couldn't the User just have a ForeignKey on countries?

shreehari Vaasistha L

unread,
Apr 17, 2020, 5:51:05 AM4/17/20
to Django users
i get this error when trying in your way:

Cannot assign "2": "User.highest_degree" must be a "Degree" instance.

Gavin Wiener

unread,
Apr 17, 2020, 5:59:34 AM4/17/20
to Django users
If you're serializing as JSON, that primary key is literally just integer then, and the foreign key relationship requires an instance of that foreign key. So you'll need to fetch an instance of the object first.

I've had this issue before, that's how I resolved it.

Denilson Antonio Avellan

unread,
Apr 17, 2020, 12:57:46 PM4/17/20
to django...@googlegroups.com
me pueden enviar el codigo completo ? para hacer un login solo la interfqaz grafica sin nada de conexion a base de datos porfavor!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c563335e-cd3f-4204-9aec-6c098dc3a525%40googlegroups.com.

Esther Camilo

unread,
Apr 17, 2020, 1:06:09 PM4/17/20
to django...@googlegroups.com
This guy has plenty of tutorials building complete applications. As front-end you can use Bootstrap in your templates



Reply all
Reply to author
Forward
0 new messages