12
Make another model that holds a string with an optional order, give it a ForeignKey
back to myClass
, and store your array in there.
13
You can use some serialization mechanism like JSON. There’s a snippet with field definition that could be of some use to you:
http://djangosnippets.org/snippets/1478/ (take a look at the code in the last comment)
With such field you can seamlessly put strings into a list and assign them to such field. The field abstraction will do the rest. The same with reading.
- [Django]-Auto-truncating fields at max_length in Django CharFields
- [Django]-How to make Django serve static files with Gunicorn?
- [Django]-Success_url in UpdateView, based on passed value
13
If you are using PostgreSQL or MongoDB(with djongo) you can do this
For PostgreSQL:
from django.contrib.postgres.fields import ArrayField
For MongoDB(with Djongo):
from djongo import models
from django.contrib.postgres.fields import ArrayField
Then
stringArr = ArrayField(models.CharField(max_length=10, blank=True),size=8)
The above works in both cases.
- [Django]-When I run test cases I get this error: psycopg2.OperationalError: cursor "_django_curs_140351416325888_23" does not exist
- [Django]-How to simplify migrations in Django 1.7?
- [Django]-How do I set user field in form to the currently logged in user?
5
I did this for my model and it worked
from django.contrib.postgres.fields import ArrayField
from django.db import models
class Skill(models.Model):
name = models.CharField(max_length=50)
skills = ArrayField(models.CharField(max_length=200), blank=True)
To create
Skill.objects.create(name='First name', skills=['thoughts', 'django'])
To Query
Skill.objects.filter(skills__contains=['thoughts'])
You can refer to the django documentation for more help
https://docs.djangoproject.com/en/3.0/ref/contrib/postgres/fields/
I hope this helps
- [Django]-Django unique_together with nullable ForeignKey
- [Django]-Django {% with %} tags within {% if %} {% else %} tags?
- [Django]-Using Sql Server with Django in production
4
You can use cPickle…
class myClass(models.Model):
title = models.CharField(max_length=50)
stringArr = models.TextField()
from cPickle import loads, dumps
data = [ { 'a':'A', 'b':2, 'c':3.0 } ]
obj = Myclass.objects.get(pk=???)
# pickle data into a string-like format
obj.stringArr = dumps(data)
obj.save()
# restore original data
data = loads(obj.stringArr)
- [Django]-Django 1.7 throws django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet
- [Django]-Extend base.html problem
- [Django]-Django manage.py Unknown command: 'syncdb'
3
You can use JSONField
for such functionality:
from django.db import models
from django.contrib.postgres.fields import JSONField
class TestModel(models.Model):
title = models.CharField(max_length=100)
strings = JSONField(default=list, blank=True, null=True)
def __str__(self):
return self.title
for example:
In [1]: fruits = ['banana', 'apple', 'orange']
In [2]: TestModel.objects.create(title='my set', strings=fruits)
Out[2]: <TestModel: my set>
In [3]: o = TestModel.objects.first()
In [4]: o.strings
Out[4]: ['banana', 'apple', 'orange']
- [Django]-Django serializer Imagefield to get full URL
- [Django]-When to use Serializer's create() and ModelViewset's perform_create()
- [Django]-Calling Django `reverse` in client-side Javascript