How to save inputs from Django forms? - django

I have a very entry level question but I could figure it out... My question is how to save inputs created by Django forms to a database (like google datastore)? It seems like there is no .save or .put functions associated with forms. Can anyone give me some suggestions or point out some examples?
Thanks!
Here is the Django forms and the website is hosted by Google app engine.
import os
os.environ['DJANGO_SETTINGS_MODULE']='settings'
from django import forms
from django.utils.safestring import mark_safe
class KabamInp(forms.Form):
chemical_name = forms.CharField(widget=forms.Textarea (attrs={'cols': 20, 'rows': 2}))
Koc = forms.FloatField(label=mark_safe('K<sub>OC</sub> (mL/g OC)'),initial=25000)

you need to create a Model class for the Form and use the values you get from the form to create your entity.

Look into the sample of Google App Engine Djnago Guest Book
It will guide thru creating a model/view/controller (MVC) application using Django on the App Engine.

Related

I was trying to use apps.get_model function. And, I want to use this without specifying the app name, is there any way to do that.

I was referring this link : Django: Get model from string?
. And, I found there is a way to do this by using apps.get_model. But,In my scenario, the model can be from other apps. So, I can't actually name the app_name here. Is there any way to do this ?
If you don't care which app the model comes from, you can do it the following way:
from django.apps import apps
def get_model_from_any_app(model_name):
for app_config in apps.get_app_configs():
try:
model = app_config.get_model(model_name)
return model
except LookupError:
pass
model = get_model_from_any_app('SomeModelName')
But in Django models in different apps can have the same name, i.e. your project can have model Post in your blog app and model Post in your news app etc.
So this way you can end up with not the model you expect, if they have duplicate names across apps (i.e. you probably should not do it this way, just think why in the world would you want a semi-random model?).
Docs which explain the code:
https://docs.djangoproject.com/en/2.0/ref/applications/#django.apps.apps.get_app_configs
https://docs.djangoproject.com/en/2.0/ref/applications/#django.apps.AppConfig.get_model

Neomodel / py2neo - 'NodeMeta' object is not iterable

I am new to Neomodel and Neo4J. I am running a remote Neo4J server on Amazon Ec2, developing locally and running into issues when trying to access the project via the browser.
When attempting to access the project via my browser, I get the following error:
TypeError at /
'NodeMeta' object is not iterable
Here is my admin.py file:
from django.contrib import admin
from app.admin import BaseAppAdmin
from .nodes import TwitterPost
class TweetAdmin(BaseAppAdmin, admin.ModelAdmin):
list_display = ('postId', 'text')
search_fields = ('text',)
admin.site.register(TwitterPost, TweetAdmin)
I have been reading through the docs in py2neo on batch read/write functionality, but I am not sure how to implement this. ( http://book.py2neo.org/en/latest/batches/#py2neo.neo4j.ReadBatch )
Any help or guidance is greatly appreciated!
I am presuming TwitterPost is a StructuredNode definition? It looks like your trying to register it with django admin however neomodel nodes don't integrate with django admin (yet) but patches are most welcome :-)
Rob

Change app label of groups in Django 1.5

I have extended the User model, but now the new user model is in an app called "account" which gives all models inside this app the app label "account". The Django model "Groups" still has the app label "Auth", so now models which all has something to do with auth is in separate apps in the admin site. Is it possibly to change the app label for "Groups"?
Try this:
from django.db.models.loading import get_models
get_models(django.contrib.auth.models)[1]._meta.app_label = 'group' #or whatever
If you need still more flexibility in the admin you could try django-admin-tools. It makes it easy to reorder and group models in different layouts (tabs, collapsible boxes, etc.) and also add dashboard-like features.
Just in case anyone needs this in Django 3.0+:
from django.apps import apps
apps.get_model('auth', 'Group')._meta.app_label = 'group' #or whatever, but have to be a registered app
Please not that this will mess with django internal model handling, e.g. generate migrations in contrib.auth sitepackage and so on

Differences between Django on AppEngine and Django on my Linux Server?

I am a Django beginner, and I want to make this tutorial as exercise: http://www.joeyb.org/blog/2009/05/28/django-based-blog-on-google-app-engine-tutorial-part-1
The thing is that this tutorial is for AppEngine, but I want to do the tutorial in my Linux Development machine using a common database.
I have noticed that there are few differences:
In the Models:
from appengine_django.models import BaseModel
from google.appengine.ext import db
class BlogPost(BaseModel):
title = db.StringProperty()
uri = db.StringProperty()
date = db.DateTimeProperty(auto_now_add=True)
teaser = db.TextProperty()
teaser_html = db.TextProperty()
content = db.TextProperty()
content_html = db.TextProperty()
tags = db.StringProperty()
These imports are different:
from appengine_django.models import BaseModel
from google.appengine.ext import db
If I change this by:
from django.db import models
It will work?
Then I noticed one more reference to AppEngine:
from google.appengine.api import users
from google.appengine.ext.db import djangoforms
What imports should I use here to make this compatible with my Django on my Linux development server?
Best Regards,
AppEngine is not Django. There are ways of getting Django to work (more or less) on AppEngine, but that tutorial is specifically for AppEngine, not Django.
If you want to learn Django, do a Django tutorial. There are enough out there on the web.

Django / Automatically retrieve models names

I'd like to retrieve automatically, in a loop, the names of the models located in a specific Django app inside my project. Does someone know how to do that ?
Best Regards
Guillaume
from django.db import get_models, get_app
app = get_app('myappname')
models = get_models(app)