I am using python 2.7.11
A have a model let's say Game that has a TextField that's supposed to store json values. TextField was chosen because the database is shared with hibernate ORM that doesn't support postgres JSONb natively. Thus I have:
models.py:
#python_2_unicode_compatible
class Game(models.Model):
settings = models.TextField(default='{}')
serializers.py:
class GameSerializer(serializers.ModelSerializer):
settings = serializers.JSONField()
Is there a clean way to handle this, having valid json strings in the database and returning them as json objects through the API?
You can try something like:
import json
#python_2_unicode_compatible
class Game(models.Model):
settings = models.TextField(default='{}')
#property
def settings_json(self):
return json.loads(self.settings)
And then in the serializer:
class GameSerializer(serializers.ModelSerializer):
settings = serializers.JSONField(source="settings_json")
Related
I'm trying to import JSONField using django import-export, it keeps saving JSON as string (adding "" to it)
models.py
from django.db import models
from django.contrib.postgres.fields import JSONField
class Governorate(models.Model):
name = models.CharField(max_length=500)
data = JSONField()
def __str__(self):
return ("%s" %(self.name))
admin.py
from django.contrib import admin
from .models import Governorate
from import_export.admin import ImportExportModelAdmin
from import_export import resources
class GovernorateResource(resources.ModelResource):
class Meta:
model = Governorate
class GovernorateAdmin(ImportExportModelAdmin):
list_display = ('id','name', 'data')
resources_class = GovernorateResource
admin.site.register(Governorate,GovernorateAdmin)
I expected the output to be: {"xx":{"xx":"xx","xx":"xx"} however it saves it as "{"xx":{"xx":"xx","xx":"xx"}"
Tried uploading XLSX and CSV.
Version 1.2.0 of import-export doesn't automatically recognise the JSONField in your Resource so it just defaults to a CharField type. It's already added to the master branch but not released.
So just override the field to use the JSONWidget which is already available in version 1.2.0:
from import_export import fields, widgets
class GovernorateResource(resources.ModelResource):
data = fields.Field(widget=widgets.JSONWidget())
class Meta:
model = Governorate
So basically, the import-export library was saving the JSONField() as a string. So my solution was to create a signal to check if the instance was a string or a dict. then fix it.
I tried to create a Field with the JSONWidget widget, but doesn't work.
models.py
#receiver(post_save, sender=Governorate)
def fix_json(sender, instance, **kwargs):
if (type(instance.data) is str):
instance.data = eval(instance.data)
instance.save()
print(instance.data)
The Django import-export library is really good, but lacks proper documentation to be honest.
THE DESCRIPTION
I'm writing an API on a Django project that will get all the LogEntry on a specific model and filter them by the specific model's field.
So my model looks like this:
from django.contrib.admin.models import ContentType, LogEntry
from django.contrib.auth.models import Group
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.db import models
class LogEntryProxy(LogEntry):
content_object = GenericForeignKey()
class Meta:
proxy = True
class HomeItem(models.Model):
# Some Fields
groups = models.ManyToManyField(Group, related_name="home_item_group")
log_entry_relation = GenericRelation(LogEntryProxy, related_query_name='log_homeitem')
In the view I need to be able to make queries that fetch LogEntryProxy items that refer to HomeItem and filter them by the groups that have access to it and serialize the results. so something like this:
log_entry_queryset = LogEntryProxy.objects.filter(log_homeitem__groups__in=user.groups.all()).distinct()
My serializer looks like this (using "djangorestframework"):
from rest_framework import serializers
class LogEntryHomeItemSerializer(serializers.ModelSerializer):
content_object = HomeItemSerializer(many=False)
object_id = serializers.IntegerField()
class Meta:
model = LogEntryProxy
fields = ('object_id', 'action_flag', 'content_object', )
And it works!
THE PROBLEM
So what's the problem you may ask!
The Problem is that LogEntry actions like create and edit work! and the API will give you the results, but when you delete a HomeItem object all the LogEntry objects pointing to it will be deleted as well, thus no delete action will be given by the api (plus all the create and edit ones pointing to that object will be deleted as well). and that's all because Django's GenericRelation doesn't support on_delete. If I delete the GenericRelatin field this doesn't happen but then I have to be able to filter the HomeItem by groups in the LogEntryProxy queryset and it can't be done without the GenericRelation.
I was wondering if anyone could tell me what to do in this situation?
Should I implement a custom logging system? or there's another way that I haven't seen it yet!
try:
homeids = HomeItem.objects.filter(groups__in=user.groups.all()).values_list('id',flat=True)
log_entry_queryset = LogEntryProxy.objects.filter(object_id__in=homeids,content_type_id=ContentType.objects.get_for_model(HomeItem).id).distinct()
If you query this way you don't need GenericRelation
Update:
The above query will not fetch logentries of delete action.
It can be done like:
from django.db.models import Q
from django.contrib.admin.models import DELETION
log_entry_queryset = LogEntryProxy.objects.filter(Q(object_id__in=home_ids,content_type_id=ContentType.objects.get_for_model(HomeItem).id) | Q(action_flag=DELETION,content_type_id=ContentType.objects.get_for_model(HomeItem).id)).distinct()
I want my models to have order field, which will contain order of an item among all items of its kind.
And I want to use choices within that IntegerField, which would contain all the numbers of currently existing items in that table.
So it would need to be dynamic choices.
How do I load all existing "order" values of all existing items in a table, and use this list for choices?
It sounds like you want to build a manager for your model:
models.py
from django.db import models
class OrderManager(models.Manager):
def order_choices(self):
return [(i, i) for i in OrderModel.objects.values_list('order', flat=True)]
class OrderModel(models.Model):
objects = OrderManager()
order = models.IntegerField()
class Meta:
ordering = ['order']
def __unicode__(self):
return '%i' % self.order
forms.py
from django import forms
from yourapp.models import OrderModel
class OrderModelForm(forms.ModelForm):
order = forms.ChoiceField(choices=OrderModel.objects.order_choices())
class Meta:
model = OrderModel
admin.py
from django.contrib import admin
from yourapp.forms import OrderModelForm
from yourapp.models import OrderModel
class OrderModelAdmin(admin.ModelAdmin):
form = OrderModelForm
admin.site.register(OrderModel, OrderModelAdmin)
Edit
Managers are use to make general model queries without having an instance of a model object. If you don't understand the concept of managers, you can still refactor the code out of the manager class, stick it somewhere else and import that function across your code. Managers allow you to abstract custom general queryset that you can reuse. See more details https://docs.djangoproject.com/en/dev/topics/db/managers/
The code without the manager will look like
views.py or some other file
from app.models import OrderModel
def order_choices():
return [(i, i) for i in OrderModel.objects.values_list('order', flat=True)]
From anywhere in your code, if you want to reuse the above multiple times:
from app.views import oder_choices
order_choices()
as opposed to:
from app.models import OderModel
OrderModel.objects.order_choices()
If you only want to use the above once, you can leave it in the forms.py as shown in the other answer. It's really up to you on how you want to refactor your code.
Dont add the choices directly to the model, add them to a form represnting the model later, by overriding the field with a set of choices.
than, do something like:
class MyForm(..):
myfield_order_field = IntegerField(choices = [(i,i) for range(MyModel.objects.count)])
class Meta():
model = MyModel
if you want to use it in the admin, add to your Admin Class:
class MyModelAdmin(admin.ModelAdmin):
...
form = MyForm
it will override this field in the admin too.
so i'm using the admin LogEntry object/table to log events in my app. I have a view where i'd like to display each LogEntry.
It would be really great if i could join the LogEntry with the actual objects they represent (so i can display attributes of the object inline with the log entry)
In theory this should be easy as we have the model type and id from the LogEntry but i can't figure out how to join them using a queryset.
i thought i could just grab all the ids of the different objects and make another dictionary for each object type and then join them somehow (maybe zip the lists together?) but that seems dumb and not very djano-ish/pythonic.
does anybody have better suggestions?
** edit **
just want to clarify am not looking to use admin, but roll a custom view and template.
As I know Django uses contenttypes framework to perform logging in admin. So you should create generic relation inside your model and then to show inlines in admin use GenericTabularInline and GenericStackedInline. Please consult with the article.
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
from django import forms
from some_app import models
from some_app.models import Item
class LogForm(forms.ModelForm):
class Meta:
model = LogEntry
class LogInline(GenericTabularInline):
ct_field = 'content_type'
ct_fk_field = 'object_id'
model = LogEntry
extra = 0
class ItemForm(forms.ModelForm):
class Meta:
model = Item
class ItemAdmin(admin.ModelAdmin):
form = ItemForm
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)
and you add to Item:
class Item(models.Model):
name = models.CharField(max_length=100)
logs = generic.GenericRelation(LogEntry)
this change won't create anything in your database, so there is no need to sync
Recent Django versions require to create a proxy for LogEntry:
from django.contrib import admin
from django.contrib.admin.models import LogEntry
from django.contrib.contenttypes.generic import GenericTabularInline
class LogEntryProxy(LogEntry):
content_object = GenericForeignKey('content_type', 'object_id')
class Meta:
proxy = True
class LogInline(GenericTabularInline):
model = LogEntry
extra = 0
class ItemAdmin(admin.ModelAdmin):
inlines = [LogInline,]
admin.site.register(models.Item, ItemAdmin)
Lets say I have the following two django (1.3) models
from django.db import models
class Patient(models.Model):
name = models.CharField('Name', max_length=50)
class Address(models.Model):
town = models.CharField('Town/Village', max_length=50)
patient = models.OneToOneField(Patient, related_name='address')
Now when i try to serialize a Patient model instance to JSON using django's serializers, the resulting JSON string does'nt have the address details with it (it's unable to traverse through the reverse direction of One-to-one relation)
This happens event if I use select_related('address') to populate the address object into the cache.
i.e.
from django.core import serializers
>>> print serializers.serialize('json',[Patient.objects.select_related('address').get(id=1)])
Is there are way I can get around this problem?
This problem came up in the project I am currently developing. Here is the solution we were going to use before we decided to just extend the Django serializer ourselves. This should work just fine for your needs though.
To solve the problem with out getting your hands dirty I would recommend wadofstuff's Django Full Serializers.
After getting it setup using the wiki solving your example would look something like this:
You need to add a method to your model which returns a python representation of the QuerySet.
from django.db import models
from django.core import serializers
class Patient(models.Model):
name = models.CharField('Name', max_length=50)
def extra_address(self):
return serializers.serialize('python', self.address.all())
class Address(models.Model):
town = models.CharField('Town/Village', max_length=50)
patient = models.OneToOneField(Patient, related_name='address')
Then you can add the method to the list of extras taken by the more robust serializer.
from django.core import serializers
print serializers.serialize('json', Patient.objects.all(), extras=('extra_address',))
An extras key will be added to the dictionary and will have whatever extra pieces of information you needed in it.