I have a django model which is basically a group called Contexts
.It contains some fields like name
, description
and a user.Below is the model defined
class Contexts(models.Model):
context_name = models.CharField(max_length=50)
context_description = models.TextField()
users = models.CharField(max_length=255, null=False)
Currently there is only one user per Context
.But I want to add more users to the same Context
.So now I want to change the users
field to an array field.By the way I am using django + postgres.
So this is what I do
class Contexts(models.Model):
context_name = models.CharField(max_length=50)
context_description = models.TextField()
users = ArrayField(ArrayField(models.TextField()))
But then how do I append users to the users
field?This is what I do normally to add a context
@csrf_exempt
def context_operation(request):
user_request = json.loads(request.body.decode('utf-8'))
if request.method == "POST":
try:
if user_request.get("action") == "add":
print("add")
conv = Contexts.objects.create(
context_name=user_request.get("context_name"),
context_description=user_request.get("context_description"),
users=user_request.get("user")
)
except Exception as e:
print("Context saving exception", e)
return HttpResponse(0)
return HttpResponse(1)
But how do I append one user at a time to the users
field in the same context (assuming same context name is passed)?