python - Django populate a class model from command, working in DB but not showing on the page -
so, problem might bit specific, not sure think missed in way fill class in commandbase.
and have @ moment : in office, in sources / articles list right. in page, articles parsed fro .csv not displayed.
here model :
class article(modelmeta, translatablemodel): taints_cache = true """ press article element, """ date_created = models.datetimefield(auto_now_add=true) date_modified = models.datetimefield(auto_now=true) date_realization = models.datefield(_('realised in'), default=timezone.now) image = filerimagefield(verbose_name=_('featured image'), blank=true, null=true, on_delete=models.set_null, related_name='image_press_article', help_text=_('set if article featured')) sources = models.manytomanyfield(articlesource, verbose_name=_('source'), blank=false, null=true, related_name='sources_press_article') regions = models.manytomanyfield(country, verbose_name=_('country of article'), blank=true, null=true, related_name='regions_press_article') global_regions = models.booleanfield('global', default=true) featurearticle = models.booleanfield(_('feature'), help_text=_('feature article'), default=false) sites = models.manytomanyfield(site, verbose_name=_('sites'), blank=true, null=true, help_text=_('select sites in show project.')) article_url = models.charfield(_('article url'), max_length=310, blank=false, help_text=_('use link original source')) countries_displayed_in = models.manytomanyfield( country, verbose_name=_('countries displayed in'), blank=true, null=true, related_name='displayed_in_press_article', help_text='select countries in project visible.' 'if not selected, project visible in countries otherwise be' 'visible users located in countries selected') translations = translatedfields( title=models.charfield(_('title'), max_length=510), slug=models.slugfield(_('slug'), blank=false, db_index=true, max_length=300), description=htmlfield(_('article description if featured'), default='', blank=true, configuration='htmlfield_ckeditor_settings_content'), meta_description=models.textfield(verbose_name=_('article meta description'), blank=true, default=''), meta_keywords=models.textfield(verbose_name=_('article meta keywords'), blank=true, default=''), meta_title=models.charfield(verbose_name=_('article meta title'), help_text=_('used in title tag , social sharing'), max_length=255, blank=true, default=''), meta={'unique_together': (('language_code', 'slug'),)} ) objects = projectmanager() _metadata = { 'title': 'get_title', 'description': 'get_description', 'keywords': 'get_keywords', 'locale': none, 'image': 'get_image_full_url', 'published_time': 'date_created ', 'modified_time': 'date_modified', # handle get_absolute_url in view have access request # , so, current_app namespace instance # 'url': 'get_absolute_url', } def country(self): return "\n".join(([p.name p in self.regions.all()])) def source(self): return "\n".join([p.name p in self.sources.all()]) def get_title(self): title = self.safe_translation_getter('meta_title', any_language=true) if not title: title = self.safe_translation_getter('title', any_language=true) return title.strip() def get_keywords(self): return self.safe_translation_getter('meta_keywords').strip().split(',') def get_description(self): description = self.safe_translation_getter('meta_description', any_language=true) if not description: description = self.safe_translation_getter('description', any_language=true) return escape(strip_tags(description)).strip() def get_image_full_url(self): if self.image: return self.image.url return '' class meta: verbose_name = _('press article') verbose_name_plural = _('press articles') get_latest_by = 'date_realization' def __str__(self): title = self.safe_translation_getter('title', any_language=true) return title if title not none else '(not translated)' def save(self, *args, **kwargs): if (self.article_url[:4] != "http") , (self.article_url[:5] != "https"): self.article_url = "https://" + self.article_url super(article, self).save(*args, **kwargs) main_lang = self.get_current_language() lang in self.get_available_languages(): self.set_current_language(lang) if not self.slug , self.title: self.slug = slugify(self.title) self.set_current_language(main_lang) self.save_translations() def get_slug(self): return self.safe_translation_getter( 'slug', language_code=get_language(), any_language=false)
and here in order add new articles, in django command :
class command(basecommand): = 'import list of press article .csv' def handle(self, *args, **options): articlefile = csv.reader(open(args[0]), delimiter=',') global_article = "" _country = "" current_site = site.objects.get_current() row in articlefile: if row[0] != "order": if row[7] == "true": global_article = "true" elif row[7] == "false": global_article = "false" _source = articlesource.objects.create(name=row[5]) logging.info("\n\n url: " + row[9] + "\n") new_article = article( article_url=row[9], global_regions=global_article, title = row[8], date_realization=datetime.strptime(row[4] + '-' + row[3] + '-' + row[2], '%y-%m-%d').date(), #sites=current_site, ) new_article.save() new_article.sources.add(_source) new_article.sites.add(current_site) logging.info("\n\n title: " + new_article.title) if row[0] == "5": break
when check db , compare articles parsing , article added back-office, difference noticed language.
for ones added hand it's : "en" , ones .csv it's "en-us".
so might problem not sure.
and if is, i don't know how "force it" 'en' instead of 'en-us'.
does has clue ? am doing right ? it's first time parse csv , populate , external model python / django.
many thanks.
wow, language problem.
i found :
from django.utils.translation import activate
i had add :
class command(basecommand): = 'import list of press article .csv' def handle(self, *args, **options): activate('en')
to code , everything's displaying correctly !
by way, still wondering if populated class right way.
feel free comment, love learn !
Comments
Post a Comment