Can't update existing record in django - python

I need to update existing recode in the database.I'm using mysql.
this my model class
class SuSurey(models.Model):
#id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase.
su_name = models.CharField(db_column='su_Name', max_length=30) # Field name made lowercase.
description = models.CharField(db_column='Description', max_length=500, blank=True) # Field name made lowercase.
duration = models.IntegerField(db_column='Duration') # Field name made lowercase.
audience = models.CharField(db_column='Audience', max_length=20, blank=True) # Field name made lowercase.
start_date = models.CharField(db_column='Start_date', max_length=50, blank=True) # Field name made lowercase.
end_date = models.CharField(db_column='End_date', max_length=50, blank=True) # Field name made lowercase.
create_date = models.CharField(db_column='Create_date', max_length=50, blank=True) # Field name made lowercase.
update_date = models.CharField(db_column='Update_date', max_length=50, blank=True) # Field name made lowercase.
create_by = models.IntegerField(db_column='Create_by', blank=True, null=True) # Field name made lowercase.
update_by = models.IntegerField(db_column='Update_by', blank=True, null=True) # Field name made lowercase.
def __unicode__(self):
return self.su_name
class Meta:
managed = False
db_table = 'su_surey'
i didn't use any forms in my web application. In my view.py i have this
if request.method == 'POST':
date=request.POST.get('dates','').split('-')
survey_data=SuSurey.objects.get(id=survey_id)
survey_data.su_name=request.POST.get('Name','')
survey_data.description=request.POST.get('Description','')
survey_data.duration=request.POST.get('Duration','')
survey_data.audience=request.POST.get('Audience','')
survey_data.start_date=date[0]
survey_data.end_date=date[1]
survey_data.update_date=datetime.datetime.today()
survey_data.update_by=request.session['user_login_data']['id']
try:
survey_data.full_clean()
survey_data.save(update_fields=['su_name','description','duration','audience','start_date','end_date','update_date','update_by'])
messages={'success':'Successfuly data added'}
url = reverse('addQuestionSurveys', kwargs={'survey_id': survey_id })
return HttpResponseRedirect(url)
except ValidationError as e:
messages={'error':e.messages}
return render(request, 'pages/forms/newSurvey.html',{'dept_data': dept_data,'messages': messages},context_instance=RequestContext(request))
when i add data Django did update the recode that is find by survey_id recode. it will add new recode to database.how can i fix this problem ? need a quick help thank you.

Related

How can I add a button inside DetailView template that sends ID to a form?

I have a App called "Properties" and I've created a DetailView that is working. Inside my Properties models I have a Property model and a Bedroom model with a ForeignKey to Property.
#views.py
class PropertyDetailView(DetailView):
template_name = 'properties/property-detail.html'
model = Property
def get_context_data(self, **kwargs):
contacts = ContactsOwner.objects.filter(owner__property=self.object)
context = super().get_context_data(**kwargs)
context['contact'] = contacts
return context
My models.py:
class Property(models.Model):
property_reference = models.CharField(db_column='Property_Reference', max_length=10) # Field name made lowercase.
address = models.CharField(db_column='Address', max_length=250, blank=True, null=True) # Field name made lowercase.
post_code = models.CharField(db_column='Post_Code', max_length=15, blank=True, null=True) # Field name made lowercase.
type = models.CharField(db_column='Type', max_length=25, blank=True, null=True, choices=HOUSE_TYPE_CHOICES) # Field name made lowercase.
bedrooms = models.IntegerField(db_column='Bedrooms', blank=True, null=True) # Field name made lowercase.
bathrooms = models.IntegerField(db_column='Bathrooms', blank=True, null=True) # Field name made lowercase.
usual_cleaning_requirements = models.CharField(db_column='Usual_Cleaning_Requirements', max_length=250, blank=True, null=True) # Field name made lowercase.
notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True) # Field name made lowercase.
feature_image = models.ImageField(null=True)
class Meta:
db_table = 'Property'
def __str__(self):
return self.property_reference
def get_absolute_url(self):
return reverse("properties:property_detail",kwargs={'pk':self.pk})
class Bedroom(models.Model):
type = models.CharField(db_column='Type', choices=BEDROOM_TYPE_CHOICES, max_length=50)
bed_dimensions = models.CharField(db_column='Bed_Dimension', choices=BED_DIMENSION_CHOICES, max_length=30)
image = models.ImageField(null=True, blank=True)
ensuite = models.BooleanField(default=False)
notes = models.CharField(db_column='Notes', max_length=500, blank=True, null=True) # Field name made lowercase.
property = models.ForeignKey(Property, null=False, on_delete=models.CASCADE, related_name='bedroom')
What I need is to create a button named "Add Bedroom" inside the template "property-detail.html" that sends me to a form with pre filled Foreign Key. Could you please help me on this?
You need to redirect that button submit to a view that gets the required info you want and then redirect that to another template with context the info you received (your Foreign key per se)

Django models and join sql tables

i wanted to try using django instead of having to call my stored procedure on the DB.
So i created a new model, taking informations from two differents models
class TbMouvementinit(models.Model):
id = models.AutoField(db_column='Id', primary_key=True) # Field name made lowercase.
dateheurecreat = models.TextField(db_column='DateHeureCreat') # Field name made lowercase.
dateheureclot = models.TextField(db_column='DateHeureClot', blank=True, null=True) # Field name made lowercase.
id_pesee = models.BigIntegerField(db_column='Id_Pesee') # Field name made lowercase.
id_proteine = models.BigIntegerField(db_column='Id_Proteine', blank=True, null=True) # Field name made lowercase.
id_humidite = models.BigIntegerField(db_column='Id_Humidite', blank=True, null=True) # Field name made lowercase.
id_espece = models.BigIntegerField(db_column='Id_Espece', blank=True, null=True) # Field name made lowercase.
id_produit = models.BigIntegerField(db_column='Id_Produit', blank=True, null=True) # Field name made lowercase.
id_codeechantillon = models.BigIntegerField(db_column='Id_CodeEchantillon', blank=True, null=True) # Field name made lowercase.
id_traitement = models.BigIntegerField(db_column='Id_Traitement', blank=True, null=True) # Field name made lowercase.
id_circuit = models.BigIntegerField(db_column='Id_Circuit', blank=True, null=True) # Field name made lowercase.
code_source = models.CharField(db_column='Code_Source', max_length=20, blank=True, null=True) # Field name made lowercase.
code_destination = models.CharField(db_column='Code_Destination', max_length=20, blank=True, null=True) # Field name made lowercase.
nomos = models.CharField(db_column='NomOS', max_length=30, blank=True, null=True) # Field name made lowercase.
codesite = models.CharField(db_column='CodeSite', max_length=9, blank=True, null=True) # Field name made lowercase.
type_mouvement = models.CharField(db_column='Type_Mouvement', max_length=3, blank=True, null=True) # Field name made lowercase.
sous_domaine = models.CharField(db_column='Sous_Domaine', max_length=1, blank=True, null=True) # Field name made lowercase.
recolte = models.SmallIntegerField(db_column='Recolte', blank=True, null=True) # Field name made lowercase.
espece = models.CharField(db_column='Espece', max_length=9, blank=True, null=True) # Field name made lowercase.
code_variete = models.CharField(db_column='Code_Variete', max_length=10, blank=True, null=True) # Field name made lowercase.
code_tiers = models.CharField(db_column='Code_Tiers', max_length=9, blank=True, null=True) # Field name made lowercase.
num_bl_livreur = models.CharField(db_column='Num_Bl_Livreur', max_length=9, blank=True, null=True) # Field name made lowercase.
num_contrat_client = models.IntegerField(db_column='Num_Contrat_Client', blank=True, null=True) # Field name made lowercase.
num_oe = models.IntegerField(db_column='Num_OE', blank=True, null=True) # Field name made lowercase.
code_transporteur = models.CharField(db_column='Code_Transporteur', max_length=9, blank=True, null=True) # Field name made lowercase.
immat_transporteur = models.CharField(db_column='Immat_Transporteur', max_length=10, blank=True, null=True) # Field name made lowercase.
poids_charge = models.FloatField(db_column='Poids_Charge', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
poids_vide = models.FloatField(db_column='Poids_Vide', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract1 = models.CharField(db_column='Code_Caract1', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract1 = models.FloatField(db_column='Val_Caract1', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract2 = models.CharField(db_column='Code_Caract2', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract2 = models.FloatField(db_column='Val_Caract2', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract3 = models.CharField(db_column='Code_Caract3', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract3 = models.FloatField(db_column='Val_Caract3', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract4 = models.CharField(db_column='Code_Caract4', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract4 = models.FloatField(db_column='Val_Caract4', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract5 = models.CharField(db_column='Code_Caract5', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract5 = models.FloatField(db_column='Val_Caract5', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract6 = models.CharField(db_column='Code_Caract6', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract6 = models.FloatField(db_column='Val_Caract6', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract7 = models.CharField(db_column='Code_Caract7', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract7 = models.FloatField(db_column='Val_Caract7', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract8 = models.CharField(db_column='Code_Caract8', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract8 = models.FloatField(db_column='Val_Caract8', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract9 = models.CharField(db_column='Code_Caract9', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract9 = models.FloatField(db_column='Val_Caract9', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_caract10 = models.CharField(db_column='Code_Caract10', max_length=5, blank=True, null=True) # Field name made lowercase.
val_caract10 = models.FloatField(db_column='Val_Caract10', blank=True, null=True) # Field name made lowercase. This field type is a guess. Modified
code_cel = models.CharField(db_column='Code_Cel', max_length=5, blank=True, null=True) # Field name made lowercase.
num_operation = models.IntegerField(db_column='Num_Operation', blank=True, null=True) # Field name made lowercase.
datetime2supv = models.CharField(db_column='DateTime2SUPV', max_length=20, blank=True, null=True) # Field name made lowercase.
num_lot_supv = models.BigIntegerField(db_column='Num_Lot_SUPV', blank=True, null=True) # Field name made lowercase.
user_supv = models.CharField(db_column='User_SUPV', max_length=18, blank=True, null=True) # Field name made lowercase.
class TbEspece(models.Model):
id = models.AutoField(db_column='Id', primary_key=True) # Field name made lowercase.
recolte = models.SmallIntegerField(db_column='Recolte') # Field name made lowercase.
groupe = models.CharField(db_column='Groupe', max_length=3) # Field name made lowercase.
categorie = models.CharField(db_column='Categorie', max_length=3) # Field name made lowercase.
code = models.CharField(db_column='Code', max_length=3) # Field name made lowercase.
libelle = models.CharField(db_column='Libelle', max_length=30) # Field name made lowercase.
class Meta:
managed = False
db_table = 'TB_Espece'
app_label = 'erp'
class ResumeMvtActif(models.Model):
mvt = models.ForeignKey(TbMouvementinit)
espece = models.ForeignKey(TbEspece)
And then call it by using just
mouvements = ResumeMvtActif.objects.all()
But of course i get a Database error because i don't really have a ResumeMvtActif table.
How can i proceed to make this work ? do i have to create an extra table on my DB ? Is there a trick to tell Django that it's not a real table and just a join of two tables ?

Django Restful Framework - Limit Results Based on Role/Group

I'm trying to build an API in Django Rest Framework (Python 3.x). As a requirement from our b2b partners we can only redistribute their content to certain users. Therefore, I'd like to edit the view to only return certain data based on a users group/role in the system.
For example, I have the following model:
class LocalDeals(models.Model):
deal_id = models.IntegerField(db_column='deal_ID', unique=True) # Field name made lowercase.
deal_url = models.CharField(db_column='deal_URL', max_length=600) # Field name made lowercase.
store_id = models.IntegerField(db_column='store_ID', blank=True, null=True) # Field name made lowercase.
store_online_id = models.IntegerField(db_column='storeOnline_ID', blank=True, null=True) # Field name made lowercase.
store_chain_id = models.IntegerField(db_column='storeChain_ID', blank=True, null=True) # Field name made lowercase.
deal_price = models.FloatField(db_column='dealPrice', blank=True, null=True) # Field name made lowercase.
deal_original_price = models.FloatField(db_column='dealOriginalPrice', blank=True, null=True) # Field name made lowercase.
deal_title = models.CharField(db_column='dealTitle', max_length=256) # Field name made lowercase.
store_name = models.CharField(db_column='storeName', max_length=64, blank=True, null=True) # Field name made lowercase.
image_small = models.CharField(db_column='smallImage', max_length=128, blank=True, null=True) # Field name made lowercase.
image_large = models.CharField(db_column='largeImage', max_length=128, blank=True, null=True) # Field name made lowercase.
store_url = models.CharField(db_column='storeURL', max_length=128, blank=True, null=True) # Field name made lowercase.
provider_name = models.CharField(db_column='providerName', max_length=64, blank=True, null=True) # Field name made lowercase.
provider_logo = models.CharField(db_column='providerLogo', max_length=128, blank=True, null=True) # Field name made lowercase.
user_provider_id = models.IntegerField(db_column='userProvider_ID', blank=True, null=True) # Field name made lowercase.
expiration_date = models.DateTimeField(db_column='expirationDate', blank=True, null=True) # Field name made lowercase.
create_date = models.DateTimeField(db_column='createDate', blank=True, null=True) # Field name made lowercase.
update_date = models.DateTimeField(db_column='updateDate') # Field name made lowercase.
yelp_rating = models.FloatField(db_column='yelp_rating',blank=True, null=True)
yelp_review_count = models.IntegerField(db_column='yelp_review_count',blank=True, null=True)
yelp_rating_img_url = models.CharField(db_column='yelp_rating_img_url',max_length=128, blank=True, null=True)
yelp_url = models.CharField(db_column='yelp_url',max_length=128, blank=True, null=True)
address1 = models.CharField(db_column='address1',max_length=64, blank=True, null=True)
city = models.CharField(db_column='city',max_length=64, blank=True, null=True)
state = models.CharField(db_column='state',max_length=32, blank=True, null=True)
zip = models.CharField(db_column='zip',max_length=10, blank=True, null=True)
country = models.CharField(db_column='country',max_length=32, blank=True, null=True)
latitude = models.FloatField(db_column='latitude',blank=True, null=True)
longitude = models.FloatField(db_column='longitude',blank=True, null=True)
deal_code = models.CharField(db_column='dealCode', max_length=32, blank=True, null=True) # Field name made lowercase.
event_date = models.DateTimeField(db_column='eventDate', blank=True, null=True) # Field name made lowercase.
is_now_deal = models.IntegerField(db_column='isNowDeal', blank=True, null=True) # Field name made lowercase.
business_type = models.IntegerField(db_column='businessType', blank=True, null=True) # Field name made lowercase.
scoring_base = models.FloatField(db_column='scoringBase', blank=True, null=True) # Field name made lowercase.
deal_type = models.CharField(db_column='dealType', max_length=16, blank=True, null=True) # Field name made lowercase.
deal_disclaimer = models.TextField(db_column='dealDisclaimer', blank=True, null=True) # Field name made lowercase.
submit_id = models.IntegerField(db_column='submit_ID', blank=True, null=True) # Field name made lowercase.
deal_date_time = models.DateField(db_column='deal_datetime',blank=True, null=True)
business_type_sub = models.IntegerField(db_column='businessTypeSub', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'local_deals'
app_label = 'api'
Which is called by the following view and serializer:
class LocalDealsViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows local_deals to be viewed.
"""
throttle_classes = (UserRateThrottle,)
queryset = LocalDeals.objects.all()
serializer_class = LocalDealsSerializer
class LocalDealsSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = LocalDeals
fields = ('deal_id','deal_url','deal_price','deal_original_price','deal_title',
'store_name','image_small','image_large','store_url','provider_name',
'expiration_date','create_date','update_date','address1','city','state',
'zip','country','latitude','longitude','deal_code',
'business_type','business_type_sub','deal_type','deal_date_time',)
If the user is not in the auth_group 'admin' (id 1) then I'd like to restrict the results so that it shows all results except where user_provider_id in [1,3]. How can I do this?
You should override get_queryset() method to restrict results based on admin auth group.
class LocalDealsViewSet(viewsets.ModelViewSet):
"""
API endpoint that allows local_deals to be viewed.
"""
throttle_classes = (UserRateThrottle,)
serializer_class = LocalDealsSerializer
def get_queryset(self):
# check here if 'list' request and user not in admin auth group
if self.action == 'list' and self.request.user not in admin auth_group:
# exclude results with 'user_provider_id' in [1,3]
return LocalDeals.objects.all().exclude(user_provider_id__in=[1,3])
# Otherwise return all results
return LocalDeals.objects.all()

Django BD error

Hi guys i´m using Django 1.7 and mysql to create an application and I´m receiving this error
ValueError at /Cannot create form field for 'radiotipo_idradiotipo' yet, because its related model u'Radioinfo' has not been loaded yet
I don´t know what is this. this error happenes on my forms.py, here´s the code:
forms.py
from django import forms
from models import Node, Datasource, Radio, Radiotipo, Snapshot, Tag, Taginfo, Valores
class NodeForm(forms.ModelForm):
class Meta:
model = Node
class DatasourceForm(forms.ModelForm):
class Meta:
model = Datasource
class RadiotipoForm(forms.ModelForm):
class Meta:
model = Radiotipo
class SnapshotForm(forms.ModelForm):
class Meta:
model = Snapshot
class TagForm(forms.ModelForm):
class Meta:
model = Tag
class TaginfoForm(forms.ModelForm):
class Meta:
model = Taginfo
class ValoresForm(forms.ModelForm):
class Meta:
model = Valores
class RadioForm(forms.ModelForm):
class Meta:
model = Radio
and here is my models.py
class Datasource(models.Model):
objects = GChartsManager()
idestacao_meteo = models.IntegerField(db_column='idDATASOURCE', primary_key=True, editable=False) # Field name made lowercase.
nome_estacao = models.CharField(db_column='NAME', max_length=45) # Field name made lowercase.
fabricante = models.CharField(db_column='MANUFACTURER', max_length=45, blank=True) # Field name made lowercase.
modelo = models.CharField(db_column='MODEL', max_length=45, blank=True) # Field name made lowercase.
node_idnode = models.ForeignKey('Node', db_column='site_idSITE', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'datasource'
class Node(models.Model):
objects = GChartsManager()
idnode = models.IntegerField(db_column='idSITE', primary_key=True, editable=False) # Field name made lowercase.
nome = models.CharField(db_column='NAME', max_length=45) # Field name made lowercase.
informacoes = models.CharField(db_column='DESCRIPTION', max_length=45, blank=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'site'
class Radio(models.Model):
objects = GChartsManager()
idradio = models.IntegerField(db_column='idRADIO', primary_key=True, editable=False)
endreal = models.CharField(db_column='ENDREAL', max_length=64, blank=True) # Field name made lowercase.
radiotipo_idradiotipo = models.ForeignKey('Radioinfo', db_column='radioInfo_idRADIOINFO') # Field name made lowercase.
datasource_idestacao_meteo = models.ForeignKey(Datasource, db_column='datasource_idDATASOURCE', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'radio'
class Radiotipo(models.Model):
objects = GChartsManager()
idradiotipo = models.IntegerField(db_column='idRADIOINFO', primary_key=True, editable=False) # Field name made lowercase.
nome = models.CharField(db_column='NAME', max_length=45)
descricao = models.CharField(db_column='DESCRIPTION', max_length=300, blank=True)
class Meta:
managed = False
db_table = 'radioinfo'
class Snapshot(models.Model):
objects = GChartsManager()
idsnap = models.IntegerField(db_column='idSNAP', primary_key=True, editable=False) # Field name made lowercase.
valor = models.FloatField(db_column='VALUE', blank=True, null=True) # Field name made lowercase.
snapshot = models.DateTimeField(db_column='SNAPSHOT', blank=True, null=True) # Field name made lowercase.
tag_idtag = models.ForeignKey('Tag', db_column='tag_idTAG') # Field name made lowercase.
class Meta:
managed = False
db_table = 'snapshot'
class Tag(models.Model):
objects = GChartsManager()
idtag = models.IntegerField(db_column='idTAG', primary_key=True, editable=False) # Field name made lowercase.
desvio = models.FloatField(db_column='DEVIATION', blank=True, null=True) # Field name made lowercase.
tempo_max = models.IntegerField(db_column='TIME_MAX', blank=True, null=True) # Field name made lowercase.
conv_rate = models.IntegerField(db_column='CONV_RATE', blank=True, null=True) # Field name made lowercase.
taginfo_idtaginfo1 = models.ForeignKey('Taginfo', db_column='tagInfo_idTAGINFO') # Field name made lowercase.
datasource_idestacao_meteo = models.ForeignKey(Datasource, db_column='datasource_idDATASOURCE', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'tag'
class Taginfo(models.Model):
objects = GChartsManager()
idtaginfo = models.IntegerField(db_column='idTAGINFO', primary_key=True, editable=False) # Field name made lowercase.
nome = models.CharField(db_column='NAME', max_length=45) # Field name made lowercase.
descricao = models.CharField(db_column='DESCRIPTION', max_length=255, blank=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'taginfo'
class Valores(models.Model):
objects = GChartsManager()
idvalores = models.IntegerField(db_column='idVALUES', primary_key=True, editable=False) # Field name made lowercase.
valor = models.FloatField(db_column='VALUE') # Field name made lowercase.
datahora = models.DateTimeField(db_column='DATETIME') # Field name made lowercase.
tag_idtag = models.ForeignKey(Tag, db_column='tag_idTAG') # Field name made lowercase.
class Meta:
managed = False
db_table = 'values'
Thanks for helping.
The radiotipo_idradiotipo is a foreign key to a model called "RadioInfo", but you don't have any model called that: only one called "Radiotipo". Is that the one you meant?
Change:
radiotipo_idradiotipo = models.ForeignKey('Radioinfo', db_column='radioInfo_idRADIOINFO') # Field name made lowercase.
By:
radiotipo_idradiotipo = models.ForeignKey('Radiotipo', db_column='radioInfo_idRADIOINFO') # Field name made lowercase.
Notice that you are using the table_name instead of the Model_Name for the foreingkey

Django filter() returning object instead of contents

Brand new to Django so excuse the newbie question. I cannot for the life of me get a Google search to return what I need.
First, I imported these using inspectdb.
Second, before providing the:
def __unicode__(self):
return u'%s %s' % (self.id, self.cuisine)
in the models, every database showed what looked like bound objects versus the actual data when looking in the admin. I assumed this was normal.
Now I'm trying to query the database and show the results. Just doing something simple, the code is:
def expand(request):
userid = Userid.objects.filter(name__contains="Test")
return render(request,'expand.html',{'userid':userid})
The return should just be Test 1, Test 2, but instead I get:
[<Userid: Userid object>, <Userid: Userid object>]
Tried userid, userid.name in the template and both return the object versus the contents.
Thanks, sorry for what I'm sure is a repeat question!
Model:
class Userid(models.Model):
id = models.BigIntegerField(primary_key=True, db_column='ID') # Field name made lowercase.
name = models.TextField()
joindate = models.DateField(db_column='joinDate') # Field name made lowercase.
visits = models.IntegerField(null=True, blank=True)
gender = models.TextField(blank=True)
address = models.TextField()
address2 = models.TextField(blank=True)
addresscity = models.TextField(db_column='addressCity') # Field name made lowercase.
addressstate = models.TextField(db_column='addressState') # Field name made lowercase.
addresszip = models.IntegerField(db_column='addressZip') # Field name made lowercase.
rating = models.IntegerField()
lastvisit = models.DateField(null=True, db_column='lastVisit', blank=True) # Field name made lowercase.
topcuisine1 = models.IntegerField(null=True, db_column='topCuisine1', blank=True) # Field name made lowercase.
topcuisine2 = models.IntegerField(null=True, db_column='topCuisine2', blank=True) # Field name made lowercase.
topcuisine3 = models.IntegerField(null=True, db_column='topCuisine3', blank=True) # Field name made lowercase.
topcuisine4 = models.IntegerField(null=True, db_column='topCuisine4', blank=True) # Field name made lowercase.
topcuisine5 = models.IntegerField(null=True, db_column='topCuisine5', blank=True) # Field name made lowercase.
dealsparticipatedin = models.IntegerField(db_column='dealsParticipatedIn') # Field name made lowercase.
privateoffersparticipatedin = models.IntegerField(db_column='privateOffersParticipatedIn') # Field name made lowercase.
privateofferssent = models.IntegerField(db_column='privateOffersSent') # Field name made lowercase.
toprestaurant1 = models.IntegerField(db_column='topRestaurant1') # Field name made lowercase.
toprestaurant2 = models.IntegerField(db_column='topRestaurant2') # Field name made lowercase.
toprestaurant3 = models.IntegerField(db_column='topRestaurant3') # Field name made lowercase.
dob = models.DateField(null=True, blank=True)
tipsrating = models.IntegerField(null=True, db_column='tipsRating', blank=True) # Field name made lowercase.
visitsweekday = models.IntegerField(null=True, db_column='visitsWeekDay', blank=True) # Field name made lowercase.
visitsweekend = models.IntegerField(null=True, db_column='visitsWeekend', blank=True) # Field name made lowercase.
reviewrating = models.IntegerField(null=True, db_column='reviewRating', blank=True) # Field name made lowercase.
spendrating = models.IntegerField(null=True, db_column='spendRating', blank=True) # Field name made lowercase.
class Meta:
db_table = 'userID'
Template:
<p>{{ userid }}</p>
You need to parse through the object elements individually.
Like this:
{% for user in userid %}
{{user}}
{% endfor %}
--OR--
{% for user in userid %}
{{user.name}}
{% endfor %}
The __unicode__ attribute is applied only to individual objects, and not a queryset. Hence the issue.
Make sure your __unicode__ is on the Userid model.
If you want to just display the name list, you can alternatively do
def expand(request):
userid = Userid.objects.filter(name__contains="Test").values_list('name', flat=True)
return render(request,'expand.html',{'userid':", ".join(list(userid))})
and your template would be just:
{{userid}}
In your Userid model, add the __unicode__ method:
class Userid(models.Model):
# all your fields
class Meta:
db_table = 'userID'
def __unicode__(self):
return unicode('{0} {1}'.format(self.name, self.id))

Categories

Resources