print referred variable from object in django/python - python

In the below python code ,can the object know that the template tag is referring a variable and get that in a python variable
newemp is the object that i am passing from the views and the template is trying to access a variable as {{newemp.get_names.emp_add}} ,now in the python code can the object print this variable i.e, emp_add
class Emp(models.Model):
name = models.CharField(max_length=255, unique=True)
address1 = models.CharField(max_length=255)
def get_names(self):
logging.debug(var)
var=self.some referred object
names = {}

No. The access is done once the appropriate object has been returned from get_names() so there is no direct way to know within the method what is being accessed.

If you are asking whether you can write to a variable within a template, and then access that value back within your Python code, I do not believe so. That kind of goes against the idea of templates, IMHO.

Related

What is the best way to access other class's variable in python?

Learning class in python, using below github(https://github.com/gurupratap-matharu/Bike-Rental-System), I cloned it and I try to add database class. I did it and it works. But I don't think I did it correctly (pythonically or object oriented correctly). Can someone please do a code review? ( https://github.com/isolveditalready/PLAYGROUND ) I am espescailly not sure if using another class's variable in another class( in dbAction.py, line 15)
Basically, I needed to access another class's variable(From class BikeRental class, variable named stock) from dbActionMe class and I didn't know how so I just passed that variable into dbActionMe class.
If code review is not possible, can someone please review below to see what I could have done differently?
db = dbActionMe()
numOfBikes = db.dbRead()
shop = BikeRental(numOfBikes)
customer = Customer()
...
db.dbUpdate(shop.stock)

Refer to created object that is assigned to a variable in Python

I have a question regarding classes in Python. When I create a new object like this and add it to a variable:
user = User.objects.create(name='The Name')
Can I use that variable to refer to that newly created object? Like this:
print(user.name)
Of course you can. Actually, it's the only way to use newly generated object - if you, for instance, did this:
User.objects.create(name='The Name')
you couldn't access that same object ever again. After that, if you do this:
print(User.objects.create(name='The Name'))
you will create another new object and print its name. So, you basically use variable to store that object you have created.

Django documentation reference

I am quite new to Django and I really don't get the documentation. For example in my code I query all available pages with Page.objects.public(). This gives me objects of type cms.models.pagemodel.Page. To use this in a template I need to know which methods I can use on this but I just cannot find the documentation and I cannot believe that I have to browse the Django source code to get to know the interface.
So does anyone know where I can find the available methods?
The Django model instance reference has some helpful stuff, but generally when working with model instances, we tend to access their data as attributes (which will be in the model definition) and we call the save method after updating these attributes.
The data types you'll get back from Django model instance attributes are all going to be Python objects, so it's usually more important to understand how to interact with those than to understand all the methods available on an instance.
To get to know a Django model, you should look at its definition. Here's an example:
class Page(models.model):
publication_date = models.DateTimeField(null=True)
name = models.CharField(max_length=255, blank=True)
For example, here, if you had a Page object with a publication_date, and that attribute was stored as a DateTimeField, then Django is going to give you a datetime object when you do:
>>> page = Page.objects.first()
>>> pubdate = page.publication_date
>>> type(pubdate)
<type 'datetime.datetime'>
Similarly, the name attribute is simply a Python string:
>>> page.name = "New page name"
>>> page.save()
# New page name is stored in the db and will be there when queried next.
Lastly, to output these things in a template, you would just refer to them in the same way:
Assuming you have a `page` variable here that is an instance...
Page Name is: {{ page.name }}
The Django book may be more helpful to familiarize yourself with interacting with Django models.

Django Mongodb ListField not saving or updating

I am starting to create a webapp using Django and MongoDB. Everything is working fine when I create a model and save it into the Database. Now, I do a "Class.objects.get()" to get the object I need from my DB and I have one field called "media" which is a ListField(). I had tried doing either:
Concert.media.append(list)
or
Concert.media.extend(list)
and then
Concert.save()
This is my "Concert" object in my models.py:
class Concert(models.Model):
main_artist = models.CharField(max_length=50)
concert_id = models.AutoField(primary_key=True)
openers = ListField(EmbeddedModelField('Opener'))
concert_date = models.DateField()
slug = models.SlugField(unique=True)
media = ListField()
And when I go to see the results in does not update the object. No values where saved. If someone can help me I going to give a super cyber fist bump.
Concert is a class, not an instance. You can't save a class. You need to make an instance of the class and save that. Something like
c = Concert()
c.media.append(list)
c.save()
(btw, just as a note, list is a bad variable name because list is a type in python. Never use types as variable names (though everyone is guilty of this at one point or another, including me.))

Property %s is required

I am write one easy program using GAE and python 2.7, but I met some problem while stored data into db. My code is below:
class MemberInfo(db.Model):
firstName = db.StringProperty(required=True)
class RegisterPageButtonDown(webapp2.RequestHandler):
def post(self):
memberInfo = MemberInfo()
memberInfo.firstName = self.request.get('firstName')
memberInfo.put()
The error raise in "memberInfo = MemberInfo()", it said "Property firstName is required". I am sure I put data in html form and the method is post, too.
I've been stuck in this problem for whole night, thanks for your reply.
You've set the firstName property to required, so when you instantiate an object you must provide that property with a value, e.g.:
memberInfo = MemberInfo(firstName = self.request.get('firstName'))
Alternatively, you can make firstName not required in your model.
The error is coming from the first line of the function, before you even get the value from the request. This is because you need to pass in that value when you instantiate the object.
firstName = self.request.get('firstName')
memberInfo = MemberInfo(firstName=firstName)
(Also note that normal naming conventions for Python mean that variables and properties are lower_case_with_underscore, not camelCase.)

Categories