Django-Haystack-Elastisearch "text" field is empty - django

After running an index_update command I get the following message:
Indexing 452 quorum_ sensings
indexed 1 - 452 of 452 (by 4672)
This is good as there are 452 rows of data in the database. However, when I check the data fields all the data fields contain data except for the "text" field. Hence, I cannot search the database using Haystack.
What would cause the "text' field to be empty? Would, textfields in the database cause this? Any help would be much appreciated!
Here is some of my code:
search-indexes.py
from bacterial.models import Quorum_Sensing
from haystack import indexes
class Quorum_SensingIndex(indexes.SearchIndex, indexes.Indexable):
text = indexes.CharField(document=True, use_template=True, template_name='quorum_sensing_text.txt')
Category_Inducer_Molecule = indexes.CharField(model_attr='Category_Inducer_Molecule', faceted=True)
Inducer_Name_Synonym_Abbreviation = indexes.CharField(model_attr='Inducer_Name_Synonym_Abbreviation', faceted=True)
Amino_Acid_Sequence_Inducer = indexes.CharField(model_attr='Amino_Acid_Sequence_Inducer', faceted=True)
Organism_Involved = indexes.CharField(model_attr='Organism_Involved', faceted=True)
Target_Protein = indexes.CharField(model_attr='Target_Protein', faceted=True)
Broad_Functional_Category_Target_Molecule = indexes.CharField(model_attr='Broad_Functional_Category_Target_Molecule',faceted=True)
def get_model(self):
return Quorum_Sensing
def index_queryset(self, using=None):
return self.get_model().objects.all()
models.py
class Quorum_Sensing(models.Model):
IUAPAC_NAME = models.CharField(max_length=400)
Inducer_Molecule = models.CharField(max_length=100)
Category_Inducer_Molecule = models.CharField(max_length=40)
Inducer_Name_Synonym_Abbreviation = models.TextField()
Chemical_Nature_Inducer_Molecule = models.CharField(max_length=40)
Natural_Synthetic = models.CharField(max_length=120)
Pubchem_Link_Inducer_Molecules = models.URLField()
Mass_Inducer_Molecule = models.CharField(max_length=50)
Number_Amino_Acids_In_Inducer_Molecule = models.CharField(max_length=60)
IUPAC_Chemical_ID = models.CharField(max_length=60)
QuorumPep_Link = models.URLField()
Amino_Acid_Sequence_Inducer = models.CharField (max_length=300)
SMILES = models.CharField(max_length=250)
Organism_Involved = models.CharField (max_length=200)
Strain1 = models.CharField(max_length=120)
Strain2 = models.CharField(max_length=120)
Strain3 = models.CharField(max_length=120)
Strain4 = models.CharField(max_length=120)
Strain5 = models.CharField(max_length=120)
Taxonomy_Link = models.URLField()
Pathogenic_Or_Not = models.CharField(max_length=120)
Host = models.CharField(max_length=120)
Target_Protein = models.CharField (max_length=160)
Target_Protein_Length = models.CharField (max_length=40)
Gene_Name = models.CharField(max_length=60)
Broad_Functional_Category_Target_Molecule = models.CharField(max_length=300)
GI = models.CharField(max_length=30)
NCBI_Link_Target_Molecule = models.URLField(max_length=400)
Mode_Of_Action = models.TextField()
UNIPROT_Link_Target_Molecule = models.URLField()
EMBL_Link_Target_Molecule = models.URLField()
Related_Citations = models.URLField(max_length=400)
Annotator = models.EmailField()
quorum_sensing_text.txt
{{ object.text }}
{{ object.Inducer_Name_Synonym_Abbreviation }}
{{ object.Category_Inducer_Molecule }}
{{ object.Amino_Acid_Sequence_Inducer }}
{{ object.Organism_Involved }}
{{ object.Target_Protein }}
{{ object.Broad_Functional_Category_Target_Molecule }}

Related

Multiple forms with one single create view in Django

I have 5 forms: MyForm, EducationForm, ExperienceForm, RecommendationForm, OtherDocumentsForm
I want to disply them in one form template. I can not do it with CreateView because it only accepts one form class. How can I create single view for multiple forms?
class MyForm(forms.ModelForm):
class Meta:
model = UserForm_uz
fields = 'all'
class EducationForm(forms.ModelForm):
class Meta:
model = Education_uz
fields = 'all'
class ExperienceForm(forms.ModelForm):
class Meta:
model = Experience_uz
fields = 'all'
class RecommendationForm(forms.ModelForm):
class Meta:
model = Recommendation_uz
fields = 'all'
class OtherDocumentsForm(forms.ModelForm):
class Meta:
model = OtherDocuments
fields = 'all'
I want all the forms to be submitted in a single request and single button. They related with foreignkey to each other EducationForm, ExperienceForm, RecommendationForm, OtherDocumentsForm connected to MyForm with foreignKey
My models:
from django.db import models
language_choices = [('1', 'Билмайман'),
('2', 'Ёмон'),
('3', 'Лугат ёрдамида'),
('4', 'Ўртача'),
('5', 'Яхши'),
('6', 'Жуда яхши'), ]
approve_choices = [('Yes', 'Ха'),
('No', 'Йўк')]
agreement_choices = [('Yes', 'Ха'),
('No', 'Йўк')]
class UserForm_uz(models.Model):
rasm = models.ImageField(upload_to='rasmlar',null=True,blank=True)
lastName = models.CharField(max_length=200)
firstName = models.CharField(max_length=200)
middleName = models.CharField(max_length=200)
birthData = models.DateField()
nation = models.CharField(max_length=50)
birthPlace = models.CharField(max_length=250)
marriage_status = models.CharField(max_length=20)
children = models.CharField(max_length=20)
militaryResp = models.CharField(max_length=150)
language_uzbek = models.CharField(choices=language_choices,max_length=150)
language_russian = models.CharField(choices=language_choices,max_length=150)
language_english = models.CharField(choices=language_choices,max_length=150)
language_boshqa = models.CharField(max_length=50)
computer_literacy = models.CharField(max_length=15)
functional_resp = models.CharField(max_length=250)
work_experience = models.CharField(max_length=200)
yutuqlar = models.CharField(max_length=200)
leaving_work_reason = models.CharField(max_length=200)
main_skills = models.CharField(max_length=300)
expected_salary = models.CharField(max_length=100)
reasontoWork = models.CharField(max_length=300)
relatives_company = models.CharField(max_length=300)
criminal_history = models.CharField(max_length=250)
homeNumber = models.CharField(max_length=15)
phoneNumber = models.CharField(max_length=15)
email = models.EmailField()
additional_info = models.CharField(max_length=300)
approve_info = models.CharField(choices=approve_choices,max_length=20)
agreement = models.CharField(choices=agreement_choices,max_length=20)
passport_file = models.FileField(upload_to='fayllar')
diplom_file = models.FileField(upload_to='fayllar')
trudovoyKnishka = models.FileField(upload_to='fayllar')
fullName = models.CharField(max_length=100)
class Education_uz(models.Model):
form = models.ForeignKey(
UserForm_uz,
on_delete=models.CASCADE,
)
startingDate = models.DateField()
endingDate = models.DateField()
name = models.CharField(max_length=200)
degree = models.CharField(max_length=50)
speciality = models.CharField(max_length=150)
diplomSeriya = models.CharField(max_length=50)
class Experience_uz(models.Model):
form = models.ForeignKey(
UserForm_uz,
on_delete=models.CASCADE,
)
startWorkDate = models.DateField()
endWorkDate = models.DateField()
name = models.CharField(max_length=100)
lavozim = models.CharField(max_length=100)
address = models.CharField(max_length=100)
class Recommendation_uz(models.Model):
form = models.ForeignKey(
UserForm_uz,
on_delete=models.CASCADE,
)
fullName = models.CharField(max_length=150)
workPlace = models.CharField(max_length=150)
phoneAndEmail = models.CharField(max_length=100)
class OtherDocuments(models.Model):
form = models.ForeignKey(
UserForm_uz,
on_delete=models.CASCADE,
)
file = models.FileField(upload_to='fayllar')
comment = models.CharField(max_length=100)
Since MyForm will be submitted at the same time as the other forms you need to exclude the ForeignKey field to UserForm_uz from all the other models, the related object doesn't exist yet so you can't select it
class EducationForm(forms.ModelForm):
class Meta:
model = Education_uz
# Repeated for all four forms
exclude = ['form'] # Whatever the ForeignKey to UserForm_uz is named
Here's an example view that uses three of the forms (I missed out two to save typing). Give each form a prefix, this reduces the risk of having form fields with conflicting names. Validate them all in one go, if any form is invalid the view should not continue. Save MyForm first and use the output to pass to the other forms as the foreign key value
def my_view(request):
if request.method == 'POST':
my_form = MyForm(request.POST, request.FILES, prefix='user')
education_form = EducationForm(request.POST, request.FILES, prefix='education')
experience_form = ExperienceForm(request.POST, request.FILES, prefix='experience')
if all([my_form.is_valid(), education_form.is_valid(), experience_form.is_valid()]):
form = my_form.save()
education = education_form.save(commit=False)
education.form = form
education.save()
experience = experience_form.save(commit=False)
experience.form = form
experience.save()
return redirect('some-view')
else:
my_form = MyForm(prefix='user')
education_form = EducationForm(prefix='education')
experience_form = ExperienceForm(prefix='experience')
return render(request, 'template.html', {'my_form': my_form, 'education_form': education_form, 'experience_form': experience_form})
In your template (template.html) you'll need to render all forms in the same form tag
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
{{ my_form }}
{{ education_form }}
{{ experience_form }}
<input type="submit" />
</form>

How to migrate existing models to Wagtail Page models

I have a existed model,which is EmsanWorks(models.Model).
I would like to have a same page model, so I copy a new model and named it EmsanWorksPage and changed models.Model to Page.
class EmsanWorksPage(Page):
id_works = models.AutoField(primary_key=True)
name_r = models.CharField(max_length=255)
name_o = models.CharField(max_length=255)
title_r = models.TextField()
title_o = models.TextField()
title_t = models.TextField()
birth_y = models.CharField(max_length=20)
birth_c = models.ForeignKey(EmsanPays, models.DO_NOTHING, db_column='birth_c',related_name='page_emsanpays_birth_c')
gender = models.CharField(max_length=6)
dest = models.CharField(max_length=255)
media_spec = models.CharField(max_length=255)
year = models.TextField() # This field type is a guess.
commission = models.CharField(max_length=255)
performer = models.CharField(max_length=255)
first_perf = models.CharField(max_length=255)
duration = models.CharField(max_length=255)
perf_c = models.ForeignKey(EmsanPays, models.DO_NOTHING, db_column='perf_c')
context = models.TextField()
instr = models.TextField()
cycle = models.CharField(max_length=255)
media_w = models.CharField(max_length=255)
setup = models.CharField(max_length=255)
prod_loc = models.TextField()
prod_per = models.TextField()
perf_tech = models.TextField()
perf_media = models.TextField()
publisher = models.CharField(max_length=255)
audio = models.CharField(max_length=255)
prog_notes = models.TextField()
reception = models.TextField()
editor = models.CharField(max_length=255)
phono = models.CharField(max_length=255)
comment = models.TextField()
timestamp = models.DateTimeField()
modif = models.TextField()
afficher = models.CharField(max_length=3)
restricted_editors = models.TextField()
class Meta:
managed = False
db_table = 'emsan_works'
However, after migrate I got this error
OperationalError at /admin/emsanapp/emsanworkspage/
(1054, "Unknown column 'emsan_works.page_ptr_id' in 'field list'")

How to show Variation as a product

I am trying to show a variation when clicked on its link it displays information as a product
here is my product model
class Product(models.Model):
brand = TreeForeignKey('Brand', verbose_name='brand', related_name='products' , default='')
category = TreeForeignKey('Category', verbose_name='Category', related_name='products' , default='')
title = models.CharField(max_length=120)
description = models.TextField(max_length = 500 ,blank=True ,null=True)
price = models.IntegerField()
active = models.BooleanField(default=True)
is_verified = models.BooleanField(default=False)
in_stock = models.BooleanField(default=False)
slug = models.SlugField(default='')
objects = ProductManager()
here is my variation model
class Variation(models.Model):
VAR_CATEGORIES = (
('size' , 'size'),
('color' , 'color'),
)
product = models.ForeignKey(Product)
category = models.CharField(max_length=120, choices = VAR_CATEGORIES)
title = models.CharField(max_length=120)
price = models.IntegerField()
discount = models.IntegerField(blank=True ,null =True)
active = models.BooleanField(default=True)
quantity = models.IntegerField(null=True , blank=True)
sales_price = models.IntegerField(blank=True ,null =True)
image = models.ImageField(upload_to=image_upload_to)
slug = models.SlugField(default='')
objects = VariationManager()
def get_absolute_url(self):
return reverse("product_detail" , kwargs={"pk":self.pk})
here is my product view
class ProductDetailView(DetailView):
model = Product
template_name = "products/product.html"
#template_name = "<appname>/<modelname>_detail.html"
here is my variation view
class VariationDetailView(DetailView):
model = Variation
template_name = "products/variation.html"
context_object_name = 'variation'
and here is the urls.py
url(r'^(?P<pk>\d+)/$' , ProductDetailView.as_view() , name = 'product_detail'),
url(r'^((?P<pk>\d+)(?:/(?P<slug>[\w\d-]+))?/$' ,VariationDetailView.as_view() , name = 'variation_detail'),
and here is the template
{% for variation in object.variation_set.all %}
<img src = "{{ variation.image.url }}">
{% endfor %}
it does show all the variations of my product but when i click on some variation then it doesnt do anything
where am i wrong?

Django Truncated incorrect DOUBLE value:

I'm attempting to pull a user's organization, and provide a queryset to on a context processor that will get filtered through multiple different filter depending on the menu selection. The current error I am getting is as titled: Truncated incorrect DOUBLE value:(followed by the org_name of one of all the organizations databased). I have no problems until I create a news article and attach an organization.
organizations model:
class Organizations(models.Model):
org_name = models.CharField(max_length=200)
org_admin = models.ForeignKey(User) #Defines the admin user account for editing
org_description = models.CharField(max_length=1000)
org_phone = models.CharField(max_length=10)
org_county = models.ForeignKey(County)
org_address = models.CharField(max_length=200, blank=True)
org_address2 = models.CharField(max_length=200, blank=True)
org_city = models.CharField(max_length=200, blank=True)
org_zip = models.CharField(max_length=10, blank=True)
org_type = models.ForeignKey(OrgType, blank=True)
sub_org = models.ForeignKey('self', null=True, blank=True) # defines a heirarchy of organiztions
org_logo = models.ImageField(upload_to = 'pic_folder/', blank=True)
org_web = models.CharField(max_length=500, blank=True)
org_facebook = models.CharField(max_length=200, blank=True)
org_twitter = models.CharField(max_length=200, blank=True)
def __unicode__(self):
return self.org_name
news model:
class News(models.Model):
title = models.CharField(max_length=200)
author = models.ForeignKey(User)
slug = models.SlugField(unique=True)
content = models.TextField()
date = models.DateTimeField(default=datetime.now)
category = models.CharField(max_length=1, choices=NEWS_CATEGORIES, blank=True)
news_orgs = models.ManyToManyField(Organizations, blank=True)
news_county = models.ManyToManyField(County, blank=False)
def __unicode__(self):
return self.title
UserOrgConnections model :
class UserOrgConnections(models.Model):
user = models.ForeignKey(User)
orgs = models.ManyToManyField(Organizations)
context processor:
def mynews(request):
now = datetime.now()
if request.user.is_authenticated():
user = request.user.get_profile()
userorgs = UserOrgConnections.objects.filter(user = user)
print userorgs.values('orgs')
county = user.county.all()
MyNews = News.objects.filter(news_orgs__org_name=userorgs)
promotions = OrgPromotion.objects.filter(organization__org_county=county)
dailyspecials = OrgDailySpecials.objects.filter(organization__org_county=county)
newsall = MyNews.all().order_by('-date')
entnews = MyNews.filter(news_county=county, category='E')
technews = MyNews.filter(news_county=county, category='T')
healthnews = MyNews.filter(news_county=county, category='H')
livingnews = MyNews.filter(news_county=county, category='L')
humornews = MyNews.filter(news_county=county, category='H')
travelnews = MyNews.filter(news_county=county, category='R')
moneynews = MyNews.filter(news_county=county, category='M')
return {
'newsall': newsall,
'now': now,
'entnews': entnews,
'technews': technews,
'livingnews': livingnews,
'humornews': humornews,
'travelnews': travelnews,
'moneynews': moneynews,
}
The template snippet:
{% for newsall in newsall %}
{% if newsall.date >= now %}
{{ newsall }}
{{newsall.date }}
{% endif %}
{% endfor %}
The following sequence of code looks suspicious:
userorgs = UserOrgConnections.objects.filter(user = user)
MyNews = News.objects.filter(news_orgs__org_name=userorgs)
This is effectively asking for a sequence of News objects whose news_orgs.org_name value is equal to the list of userorgs. You haven't listed the model for UserOrgConnections so it's not possible to determine a solution with the information provided.
Perhaps you should look at the Django Queryset in operator which is capable of performing a query which filters on a list.

Rendering a ManyToMany Field

I'm trying to display a manytomany field in my template, but all I get is a blank...I'm displaying it as below:
{% for vehicle in vehicle.features.features %}
<li>vehicle.features</li>
{% endfor %}
My model is as follows:
class Vehicle(models.Model):
stock_number = models.CharField(max_length=6, blank=False)
vin = models.CharField(max_length=17, blank=False)
common_vehicle = models.ForeignKey(CommonVehicle)
exterior_colour = models.ForeignKey(ExteriorColour)
interior_colour = models.ForeignKey(InteriorColour)
interior_type = models.ForeignKey(InteriorType)
odometer_unit = models.ForeignKey(OdometerUnit)
status = models.ForeignKey(Status)
odometer_reading = models.PositiveIntegerField()
selling_price = models.PositiveIntegerField()
purchase_date = models.DateField()
sales_description = models.CharField(max_length=60, blank=False)
feature_sets = models.ManyToManyField(FeatureSet, blank=True)
features = models.ManyToManyField(Feature, blank=True)
def __unicode__(self):
return self.stock_number
The classes I'm linking to are:
class Feature(models.Model):
name = models.CharField(max_length=32)
type = models.ForeignKey(FeatureType)
def __unicode__(self):
return self.name
class FeatureSet(models.Model):
name = models.CharField(max_length=12)
features = models.ManyToManyField(Feature)
def __unicode__(self):
return self.name
Use this:
{% for feature in vehicle.features.all %}
<li>{{ feature }}</li>
{% endfor %}