Update dictionary value in Django Updateview - django-models

I have a char field that is inside a model in Django. The data I save in that field is in dictionary format, just like this {'name': 'Jon', 'age': 25}. How can I update that using Django updateview?

Related

Firestore many to many relationships - How to update data?

Like a note app, you can set tags to a note. Also, you can search notes with tag.
Here is the firestore structure:
notes(collection)
note_1(doc)
content: 'some words...'
tags: ['apple', 'banana']
tags(collection)
tag_1(doc)
name: 'apple'
tag_2(doc)
name: 'banana'
If I change tag_1.name to 'new_apple', how shold I update note_1.tags?
You're going to have to:
Query for all notes documents where the tags array contains "apple"
Iterate the documents in the result set. For each document:
Update update the tags array in memory to remove "apple" and add "new_apple"
Write the new tags array back to the document

How to create objects that have relationships when using Backand BaaS?

I am using Backand BaaS for my ionic app. My model consists of two tables:
1. Users
2. Trips
A one-to-many relationship has been established between 'Users' and 'Trips'.
'Users' has a collection field that is a collection of 'Trips' and 'Trips' has a owner field that is an object of 'Users'.
What is the correct way to create an object so that upon creating I am assigning the correct owner ('Users' object) to 'Trips' collection field ?
When POSTing a new Trip use this for the Trip Object
{
tripName:"asdf"
owner:1
}
In the users object it will look like this
{
id:1
fName:"david"
trips:[{__metadata: {id: 1}, tripName: "asdf"}]
}

How to generate the right data structure for ngoptions in single request

I'm trying to return a data structure for an Angular screen with several dropdowns.
I don't want to make multiple requests to get the options for each select so I was hoping to create a single nested data structure in Django that contains all the options for each select (there are only a small number)
Angular wants something like this to use with the select and ngoptions directives:
{'booking_name': 'acme',
'current_sales_person': 2,
'sales_people_options': [
{id: 1, name: 'rod'},
{id: 2, name: 'jane'},
{id: 3, name: 'freddy'}
],
... lots more fields here ...
}
sales_people_options would populate the options for the dropdown and current_sales_person indicates which salesperson is initially selected.
How can I get Django Rest Framework to return this in a single request from a ModelSerializer?
I could just create the JSON directly via JSONRenderer but I've got a lot of other fields so I'm keen to use ModelSerializer.
The only way seems to be to add a method called sales_people_options to my Model.
However - I've got several of these fields to generate and it seems very wrong to add model methods for something that I only need for one specific serializer.
EDIT - I think I might need to subclass serializer.Field and override a method (probably field_to_native).
This is just a json object. The Django rest framework is capable of returning json strings which you can then JSON.stringify the string returned to turn it into an object angular is expecting.
{'booking_name': 'acme',
'current_sales_person': 2,
'sales_people_options': [
{id: 1, name: 'rod'},
{id: 2, name: 'jane'},
{id: 3, name: 'freddy'}
Checkout the Django JSON renderer; http://www.django-rest-framework.org/api-guide/renderers
This might be a weird way to do it but it worked:
class BookingSerializer(serializers.ModelSerializer):
class UserOptionsField(serializers.Field):
def field_to_native(self, obj, field_name):
return User.objects.all().values('id', 'username')
sales_people_options = UserOptionsField()
class Meta:
model = Booking
fields = (
'id', 'booking_no', 'current_salesperson', 'sales_people_options',
)
EDIT: And here's a generic solution:
class ModelOptionsField(serializers.Field):
def __init__(self, source=None, label=None, help_text=None, queryset=None, fields=None):
self.queryset = queryset
self.fields = fields
super(ModelOptionsField, self).__init__(source, label, help_text)
def field_to_native(self, obj, field_name):
return self.queryset.values(*self.fields)
that you use like this:
class MySerializer(serializers.ModelSerializer):
my_options_list = ModelOptionsField(queryset=User.objects.all(), fields=('id', 'username'))

Marionette - backbone how to use fetch correctly

This is a simple question but I can's see it answered in the Marionette Wiki.
If I load data into a Collection via a url what format does that data have to be in?
Does it have to have an ID like this:
[
{id: "something", name: "Justin", hobbies: ""},
{id: "something2", name: "Fred", hobbies: ""}
]
Is the ID field required for it to work properly?
Yes, you should have an id attribute: Backbone determines whether the model instance has been persisted on the remote server by checking whether it has an id value or not.
if it has an id => use HTTP PUT verb to cause an update
if it doesn't have an id => use HTTP POST verb to create a new record
We do not use id field in our data set so it is not a 'must'. We also manage when to use POST or PUT directly from the client code and not relying on backbone to do it automatically.
model.save(..., {type: 'POST'});
model.save(..., {type: 'PUT'});

Drupal 7 - Content type inside a content type - Like field collection

I´d like to create a content type called "contact" that will be share in other content types, like "client" and "prospect". When I create a new client, I´d like to be able to create, in the same form, new contacts for this client, like a field collection. For example:
ADD NEW CLIENT
Name:
Firm:
Email:
Contacts (multiple value)
Name:
Email:
Add new contact
But, in the "Contact" content type I have a lot more fields that I can fill up later:
Name:
Email:
Phone:
Address:
City:
State....
Thanks!
You could use the rules module to setup a rule that whenever a new 'Client' entity is created a new 'Contact' entity is created based on the values in the client contacts fields (name and email).
However I am unsure as to how you could implement an 'add another item' for field groups in a form (which is what I assume is the desired functionality of your 'Add new contact').
You may also want to make sure to include an entity reference field on your contact entity type, so that it can reference the client entity to which it is related. That would give you the option in the future to display a view of contacts related to a client.

Resources