<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ed Kohler on Medium]]></title>
        <description><![CDATA[Stories by Ed Kohler on Medium]]></description>
        <link>https://medium.com/@edkohler?source=rss-64a426d4acf------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*tGF57A9YuF4lxIUH.png</url>
            <title>Stories by Ed Kohler on Medium</title>
            <link>https://medium.com/@edkohler?source=rss-64a426d4acf------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 20:20:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@edkohler/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Creating Amazon Polly Voice Samples with MoviePy]]></title>
            <link>https://medium.com/@edkohler/creating-amazon-polly-voice-samples-with-moviepy-8df140f96670?source=rss-64a426d4acf------2</link>
            <guid isPermaLink="false">https://medium.com/p/8df140f96670</guid>
            <category><![CDATA[polly]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[moviepy]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[text-to-speech]]></category>
            <dc:creator><![CDATA[Ed Kohler]]></dc:creator>
            <pubDate>Thu, 25 Jul 2024 05:17:58 GMT</pubDate>
            <atom:updated>2024-07-25T05:17:58.845Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*KtuyWNPa-Uc_qZUzcryb3g.png" /></figure><p>I wanted to use a text-to-voice system to generate audio clips for a project that was going to use <a href="https://aws.amazon.com/polly/">AWS Polly</a> for voice generation but didn’t find a good showcase of the various voices so I created my own using the Python package, <a href="https://zulko.github.io/moviepy/">MoviePy</a>.</p><p>Here’s how that came together.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FbWwYeQx9AFo%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DbWwYeQx9AFo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FbWwYeQx9AFo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fdd44c856de04008ab6e0ac67ed588c/href">https://medium.com/media/0fdd44c856de04008ab6e0ac67ed588c/href</a></iframe><p>I have a previous script that used Amazon Polly to read quiz show voiceovers so used that as my starting point. Rather than asking questions and answers, I used the question slide to read a demonstration of each Polly voice.</p><p>I compiled the various voice options in a spreadsheet that would become a csv file for the script. Amazon needs to know the language code, voice name, and voice engine in a Polly request. Some voices (voice names) are available in different voice engines so the same voice may sound more polished if you choose different voice engines. The Language and Gender fields were used to concatenate together a script for each voice.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fft8yq8hRxXlctKDhQD7Jg.png" /></figure><p>Here’s the function that passed the text request to Amazon Polly together with the other necessary variables:</p><pre>def generate_text_to_speech(index, text, amazon_voice_name, amazon_voice_engine):<br>    try:<br>        response = polly.synthesize_speech(<br>            Text=text,<br>            OutputFormat=&#39;mp3&#39;,<br>            VoiceId=amazon_voice_name,<br>            Engine=amazon_voice_engine<br>        )<br>    except (BotoCoreError, ClientError) as error:<br>        print(error)<br>        sys.exit(-1)<br><br>    if &#39;AudioStream&#39; in response:<br>        audio_path = os.path.join(folder_name, f&quot;{index}.mp3&quot;)<br>        with open(audio_path, &#39;wb&#39;) as file:<br>            file.write(response[&#39;AudioStream&#39;].read())<br>            print(file.name)<br>            return file.name<br>        print(&quot;The audio file has been saved.&quot;)<br>    else:<br>        print(&quot;Could not stream audio from AWS Polly&quot;)</pre><p>Here’s a list of the imported packages. Some may not be used in this code because they applied to image resizing and other stuff that was stripped out of the previous code for this script.</p><pre>import os<br>import pandas as pd<br>import numpy as np<br>from moviepy.editor import VideoFileClip, concatenate_videoclips, TextClip, AudioFileClip, CompositeVideoClip, ColorClip, vfx, VideoClip, ImageClip, CompositeAudioClip, afx<br>import uuid<br>from moviepy.video.fx.resize import resize<br>from moviepy.audio.fx.volumex import volumex<br>import boto3<br>from botocore.exceptions import BotoCoreError, ClientError<br>import sys<br>import psutil<br>import gc<br>from datetime import datetime<br>import requests<br>from urllib.parse import urlparse</pre><p>The first clip in the MoviePy video was a 5-second audio clip. This involves passing the title of the video to Amazon to convert to audio along with the design work for layout out the text, text shadow, text background color, and background image. The layout work is the most tedious part of making videos with MoviePy.</p><pre># Create an empty list to store video clips<br>video_clips = []<br><br># Create an intro clip<br>intro_text_audio = generate_text_to_speech(&quot;intro_text_audio&quot;, quiz_name, &quot;Ruth&quot;, &quot;long-form&quot;)<br><br>intro_clip = TextClip(txt=quiz_name, color=&#39;white&#39;, font=font, fontsize=70).set_duration(intro_clip_duration)<br>intro_clip = intro_clip.set_position((&#39;center&#39;, &#39;center&#39;))<br>intro_clip_size = intro_clip.size<br><br>intro_clip_shadow = TextClip(txt=quiz_name, color=&#39;black&#39;, font=font, fontsize=70).set_duration(intro_clip_duration).set_position(<br>    lambda t: (intro_clip.size[0]/2 - intro_clip.size[0]/2 + 24,<br>               intro_clip.size[1]/2 - intro_clip.size[1]/2 + 16)<br>)<br>intro_width, intro_height = intro_clip.size<br><br>intro_clip_background = ColorClip(size=(intro_width + 40, intro_height + 20), color=intro_clip_text_background, duration=intro_clip_duration).set_position((&#39;center&#39;, &#39;center&#39;))<br>intro_clip_background = intro_clip_background.set_opacity(0.8)<br><br>intro_clip_video = CompositeVideoClip([intro_clip_background, intro_clip_shadow, intro_clip])<br>intro_clip_video = intro_clip_video.set_position((&#39;center&#39;, &#39;center&#39;))<br>intro_clip_video = CompositeVideoClip([ColorClip(size=(width, height), color=(0,0,0,0)).set_duration(5)] + [intro_clip_video], size=(width, height))<br><br>intro_audio_clip = AudioFileClip(f&quot;{folder_name}/intro_text_audio.mp3&quot;)<br>intro_clip_video = intro_clip_video.set_audio(intro_audio_clip)<br><br>video_clips.append(intro_clip_video)</pre><p>Then there’s the MoviePy video clip that will populate using each line of the csv. The design work is handled. A request to the Polly function is sent to generate the mp3. That mp3 audio is attached to the video, and the video is appended to the video file. I used the length of the audio file returned from Polly + 2 seconds for the length of each video clip.</p><p>For those not familiar with MoviePy, you create each of the elements that will make up a segment of a video, like video clips, text, colors, and where everything should appear on the screen, then you put those elements together using CompositeVideoClip. Audio is added using the set_audio function.</p><p>Then I added an outro clip and appended that to the video:</p><pre>outro_text = &quot;Thanks for playing!\n\nDon&#39;t forget to like and subscribe.&quot;<br>outro_text_audio = generate_text_to_speech(&quot;outro_text_audio&quot;, outro_text, &quot;Ruth&quot;, &quot;long-form&quot;)<br><br>outro_clip = TextClip(txt=outro_text, color=&#39;white&#39;, font=font, fontsize=70).set_duration(intro_clip_duration)<br>outro_clip = outro_clip.set_position((&#39;center&#39;, &#39;center&#39;))<br>outro_clip_size = outro_clip.size<br><br>outro_clip_shadow = TextClip(txt=outro_text, color=&#39;black&#39;, font=font, fontsize=70).set_duration(intro_clip_duration).set_position(<br>    lambda t: (outro_clip.size[0]/2 - outro_clip.size[0]/2 + 24,<br>               outro_clip.size[1]/2 - outro_clip.size[1]/2 + 16)<br>)<br>outro_width, outro_height = outro_clip.size<br><br>outro_clip_background = ColorClip(size=(outro_width + 40, outro_height + 20), color=intro_clip_text_background, duration=intro_clip_duration).set_position((&#39;center&#39;, &#39;center&#39;))<br>outro_clip_background = outro_clip_background.set_opacity(0.8)<br><br>outro_clip_video = CompositeVideoClip([outro_clip_background, outro_clip_shadow, outro_clip])<br>outro_clip_video = outro_clip_video.set_position((&#39;center&#39;, &#39;center&#39;))<br>outro_clip_video = CompositeVideoClip([ColorClip(size=(width, height), color=(0,0,0,0)).set_duration(5)] + [outro_clip_video], size=(width, height))<br><br>outro_audio_clip = AudioFileClip(&quot;outro_text_audio.mp3&quot;)<br><br># Calculate the total duration of all video clips (excluding the outro for now)<br>total_duration = sum(clip.duration for clip in video_clips)<br><br># Set the start time for the outro clip<br>outro_clip_video = outro_clip_video.set_start(total_duration)<br>outro_clip_video = outro_clip_video.set_audio(outro_audio_clip.set_start(total_duration))<br>print(f&quot;Outro video duration: {outro_clip_video.duration}&quot;)<br><br><br># Append the outro clip to the list of video clips<br>video_clips.append(outro_clip_video)</pre><p>After that, it was time to wrap up the video. I added a small background audio the full video, the picture of the parrot to the background, concatenated together the list of video clips that were generated in earlier steps, and generated the final video as an mp4.</p><pre><br># Update the background clip duration to match the total duration of all clips<br>total_video_duration = total_duration + outro_clip_video.duration<br>print(total_video_duration)<br><br># Calculate the total video duration<br>total_video_duration = sum(clip.duration for clip in video_clips)<br><br># Loop the background audio and adjust its volume<br>background_audio = afx.audio_loop(background_audio, duration=total_video_duration)<br>background_audio = background_audio.volumex(0.2)<br><br># Loop the background video to match the total video duration<br>looped_background = background_clip.fx(vfx.loop, duration=total_video_duration)<br><br># Concatenate all the video clips into a single video<br>final_clip = concatenate_videoclips(video_clips)<br><br># Combine the background audio with the final clip&#39;s audio<br>final_audio = CompositeAudioClip([final_clip.audio, background_audio.set_duration(final_clip.duration).volumex(0.1)])<br>final_clip = final_clip.set_audio(final_audio)<br><br># Create the final composite video with the looped background<br>final_composite = CompositeVideoClip([looped_background, final_clip])<br><br># Write the final video to a file<br>final_composite.write_videofile(new_filename, codec=&#39;libx264&#39;, audio_codec=&#39;aac&#39;, fps=24)<br><br>print(f&quot;Video saved as {new_filename}&quot;)</pre><p>Now that it’s working, I could update the spreadsheet with different scripts if I wanted to test the voices for different applications using different text.</p><h3>Polly Thoughts</h3><p>Regarding Polly, the biggest trick is to make sure that you update the voice name AND the voice engine when updating your voice preferences because different voices use different engines. And, not all voices are available in all Amazon AWS regions so you may need to update the region your requesting results from based on the voice you choose. The more sophisticated voices have fewer deployments than standard voices.</p><h3>MoviePy Thoughts</h3><p>This is a powerful package. I don’t have a lot of experience with movie editing, which slowed down my template layout time. However, I didn’t have to unlearn ways of doing that with other software or code.</p><p>I can see how this package would be very useful for doing things like pulling together content from a variety of sources to generate clips optimized for different video platforms. A YouTube, TikTok, Reels, etc. versions of content could be generated by creating templates for each platform / layout and feeding in the text, text-to-voice, and supporting video/image content.</p><p>The documentation was pretty good, with examples of different things that can be done. However, the public internet’s knowledge base about MoviePy is somewhat limited, so I had a hard time finding MoviePy-specific answers to problems I encountered using StackOverflow or AI tools.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8df140f96670" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Break Django Models into Smaller Models with Shared Fields]]></title>
            <link>https://medium.com/@edkohler/how-to-break-django-models-into-smaller-models-with-shared-fields-47d059fcc236?source=rss-64a426d4acf------2</link>
            <guid isPermaLink="false">https://medium.com/p/47d059fcc236</guid>
            <category><![CDATA[django-models]]></category>
            <category><![CDATA[database-design]]></category>
            <category><![CDATA[django]]></category>
            <dc:creator><![CDATA[Ed Kohler]]></dc:creator>
            <pubDate>Fri, 10 May 2024 14:16:16 GMT</pubDate>
            <atom:updated>2024-05-10T14:16:16.554Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s say that you’re using Django to build a catalog, and the catalog has products with a variety of different specs. An example of this could be a library catalog that has books but also audiobooks, and audiobooks have fields unique to that medium such as narrator and duration.</p><p>Here’s a <a href="https://www.reddit.com/r/django/comments/1cmv427/when_to_break_a_model_into_smaller_models/">quote</a> from someone on Reddit describing a similar challenge they’re facing with a site they’re building:</p><blockquote>I am fairly new to Django and was curious about how best to set up a model in my webapp. I’m messing around and trying to build something akin to PCPartPicker. When I started, I just had a model called “Parts” where all the parts were stored. I quickly realized that if I wanted to account for every specification each part might have, the list of fields in the model would be in the hundreds. There are many specs that apply to all parts such as mass, dimensions, and price, but there are many that are only applicable to one part type (e.g. fan speed, or case material).</blockquote><p>In a situation like that, one could create a massive model that has tons of unused fields depending on which product is listed. Or, one could create lots of unique models with many redundant fields for universal specs like weight and product name. But, there is a better way: model inheritance.</p><p>With model inheritance, you can create a base model with the common fields that will apply to most items. For a product catalog, this could include fields like name, brand, UPC, weight, color, images, etc. Then, you can create category-specific models that inherit the base model and build upon that with fields that are unique to that category.</p><p>Building off the PCPartPicker example, a field like “fan speed” would only be relevant to parts that have fans. A “Fan” model could be created that lists fields unique to fans while inheriting the base model for fields that apply to all products.</p><h3>Multi-table inheritance</h3><p>Avoiding redundancies or extraneous fields is done through model inheritance. There are a variety of different approaches to this including <a href="https://docs.djangoproject.com/en/5.0/topics/db/models/#model-inheritance">model inheritance</a> and <a href="https://docs.djangoproject.com/en/5.0/topics/db/models/#multi-table-inheritance">multi-table inheritance</a>. One difference between the two is whether the base fields are abstract (model inheritance) or reside in their own table (multi-table inheritance). In the example below I’ll focus on multi-table inheritance.</p><p>The code below is an example of what could be created for a Product model in a multi-table inheritance setup. Notice that the fields listed below are relatively generic. They cover the specs that are relevant to all products. Also, note that there are some administrative fields here as well, such as when the product was added to the database, last updated, and whether it’s an active product or not (so it could be in the database but not publicly displayed).</p><pre>class Product(models.Model):<br>    name = models.CharField(max_length=240, null=True, blank=True)<br>    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, null=True)<br>    model_number = models.CharField(max_length=100, null=True, blank=True)<br>    model_name = models.CharField(max_length=100, null=True, blank=True)<br>    upc = models.CharField(max_length=14, null=True, blank=True, unique=True)<br>    category = models.CharField(max_length=1000, choices=CATEGORY_OPTIONS)<br>    description = models.TextField(null=True, blank=True)<br>    color = models.ManyToManyField(Color, blank=True, related_name=&#39;products&#39;)<br>    weight = models.IntegerField(help_text=&quot;Weight in grams.&quot;, blank=True, null=True)<br>    created_at = models.DateTimeField(auto_now_add=True, auto_now=False)<br>    updated_at = models.DateTimeField(auto_now_add=False, auto_now=True)<br>    active = models.BooleanField(default=True)</pre><p>Here’s an example of a model that inherits the Product model and builds upon that with category-specific fields. This example comes from a bike parts website. Specifically, fields relevant to a bike’s rear derailleur.</p><pre>class RearDerailleur(Product):<br>    speeds = models.ManyToManyField(<br>        CompatibleSpeeds, blank=True, related_name=&quot;rear_derailleurs_speeds&quot;<br>    )<br>    CASSETTE_RING_CHOICES = [(i, i) for i in range(9, 60)]<br>    cage_length = models.CharField(<br>        max_length=1, choices=CAGE_LENGTH_CHOICES, null=True, blank=True<br>    )<br>    min_gear = models.IntegerField(choices=CASSETTE_RING_CHOICES, null=True, blank=True)<br>    max_gear = models.IntegerField(choices=CASSETTE_RING_CHOICES, null=True, blank=True)<br>    total_capacity = models.IntegerField(<br>        choices=CASSETTE_RING_CHOICES, null=True, blank=True<br>    )<br>    low_sprocket_max = models.IntegerField(<br>        choices=CASSETTE_RING_CHOICES, null=True, blank=True<br>    )<br>    top_sprocket_min = models.IntegerField(<br>        choices=CASSETTE_RING_CHOICES, null=True, blank=True<br>    )<br>    pully_teeth = models.ManyToManyField(<br>        CompatibleSpeeds, blank=True, related_name=&quot;rear_derailleurs_pully_teeth&quot;<br>    )<br>    cable_pull_ratio = models.ForeignKey(<br>        CablePullRatio, on_delete=models.CASCADE, blank=True, null=True<br>    )<br>    mount_type = models.ManyToManyField(RearDerailleurMounts, blank=True)<br>    actuation = models.CharField(<br>        max_length=1,<br>        choices=DERAILLEUR_ACTUATION_CHOICES,<br>        blank=True,<br>        null=True,<br>        help_text=&quot;Cable, Wireless&quot;,<br>    )<br><br>    clutch = models.CharField(<br>        max_length=1, choices=CLUTCH_CHOICES, default=&quot;U&quot;, null=True, blank=True<br>    )</pre><p>Imagine how many fields would be in the Product table if the above fields were added there rather than in a separate category-specific model. It would quickly get out of control and be confusing for people entering product data because it wouldn’t be clear which fields were relevant to a given product.</p><p>What does this look like on the back end? Here’s an example. In this case, I’ve clicked into a rear derailleur record in the Admin section of the site. The first fields displayed are the fields from the Product table. Scrolling down the page shows the category-specific field fields for rear derailleurs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-_goctIAB86B9-KV2TsTOQ.png" /><figcaption>Product Fields for a rear derailleur.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*G6DoGIScGWjwGBlYDJ8_dg.png" /><figcaption>Fields that apply specifically to rear derailleurs.</figcaption></figure><p>A great benefit to taking this approach is being able to easily build forms and filters that are category-specific. This makes it much easier to see where you have fields missing in your database because you can define which fields are must-haves for each category.</p><p>I hope you found this helpful. If you made it this far, give me a clap or buy me a coffee.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47d059fcc236" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Displaying ManyToManyField Info in Django Admin]]></title>
            <link>https://awstip.com/displaying-manytomanyfield-info-in-django-admin-07566810ea78?source=rss-64a426d4acf------2</link>
            <guid isPermaLink="false">https://medium.com/p/07566810ea78</guid>
            <category><![CDATA[django-admin]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[many-to-many-relationship]]></category>
            <dc:creator><![CDATA[Ed Kohler]]></dc:creator>
            <pubDate>Wed, 01 May 2024 14:11:04 GMT</pubDate>
            <atom:updated>2024-05-06T16:21:04.064Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/874/1*9ZYBs4gczp0VYLjzL132bg.png" /><figcaption>Adding a ManyToManyField to Django Admin tables</figcaption></figure><p>Displaying data from a model in Django Admin is a pretty simple process, most of the time. For example, here’s a simple admin.py setup for a Product model that displays four categories when logged into the Admin console.</p><pre>class ProductAdmin(admin.ModelAdmin):<br>    model = Product<br>    list_display = (&quot;name&quot;, &quot;brand&quot;, &quot;model_number&quot;,&quot;category&quot;)</pre><p>In the above case, name and model_number are text fields, brand is a foreign key to brands and category is a choices field. To display fields like this you simply list them in list_display.</p><p>But, things get a bit more complicated if you want to display the content of ManyToManyField fields.</p><p>To display ManyToManyField information we need to look up the values, put them into a list, assign them a field name, then add that field name to list_display. That can be done with three lines of code. For example, I have a product model where one field is color. A product may have more than one color so color is a ManyToManyField for this project. To display the colors assigned to a product in Django Admin, I added the following function to the ProductAdmin settings in admin.py:</p><pre>class ProductAdmin(admin.ModelAdmin):<br>    model = Product<br>    list_display = (&quot;name&quot;, &quot;brand&quot;, &quot;model_number&quot;,&quot;category&quot;, &quot;colors&quot;)<br><br>    @admin.display(description=&#39;colors&#39;)<br>        def colors(self, obj):<br>            return [color.color_name for color in obj.color.all()]</pre><p>The list comprehension in the last line looks up matching “color_name” values in the Color model and turns them into a list called “colors”. The last step is to add the new field to list_display (I added it to the end of the list_display tuple above).</p><p>Here’s what the newManyToManyField looks like within the Product model’s Django Admin display:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/194/1*cu1uY3bDmEYTGKBCJdt_Jg.png" /><figcaption>List of colors in Django Admin.</figcaption></figure><p>Yes, you can do this for more than one field in a model.</p><p>Adding ManyToManyField data to Django Admin tables can get a little computationally expensive if you have large models since Django is running an additional query for each row of data (or more if you’ve added more than one lookup like this).</p><p>For the project where I’m using this approach, I was looking for a quick and easy way to identify missing data in product tables. Noticing holes where colors are supposed to be listed is very obvious in the admin interface now.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=07566810ea78" width="1" height="1" alt=""><hr><p><a href="https://awstip.com/displaying-manytomanyfield-info-in-django-admin-07566810ea78">Displaying ManyToManyField Info in Django Admin</a> was originally published in <a href="https://awstip.com/">AWS Tip</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving Datetime Import Headaches with django-import-export Widgets]]></title>
            <link>https://medium.com/@edkohler/solving-datetime-import-headaches-with-django-import-export-widgets-e948e750e5c5?source=rss-64a426d4acf------2</link>
            <guid isPermaLink="false">https://medium.com/p/e948e750e5c5</guid>
            <category><![CDATA[django-admin]]></category>
            <category><![CDATA[django]]></category>
            <category><![CDATA[python-datetime]]></category>
            <category><![CDATA[django-import-export]]></category>
            <dc:creator><![CDATA[Ed Kohler]]></dc:creator>
            <pubDate>Fri, 22 Mar 2024 21:49:34 GMT</pubDate>
            <atom:updated>2024-03-22T21:49:34.260Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s say that you’ve installed django-import-export and and super excited to upload your first .csv or other text file of data into one of your Django project’s models.</p><p>Things seem to be going well until you find out that you’re getting an error on every row of data due to a conflict between how dates are formatted in your file and how they’re formatted in your Django model.</p><figure><img alt="Some rows failed to validate Please correct these errors in your data where possible, then reupload it using the form above. CREATED_AT Enter a valid date/time." src="https://cdn-images-1.medium.com/max/500/1*o_XzZmE3DPWDFpfr5RP8tA.png" /><figcaption>Some rows failed to validate<br>Please correct these errors in your data where possible, then reupload it using the form above. CREATED_AT<br>Enter a valid date/time.</figcaption></figure><p>In this case, the datetime data in the csv file being imported looks like this:<br>6/18/2012, 5:53:59 PM<br>while Django was looking for this:<br>2012–06–18 17:53:59</p><p>So, how do you solve this? Enter django-import-export’s <a href="https://django-import-export.readthedocs.io/en/latest/api_widgets.html#import_export.widgets.DateTimeWidget">DateTimeWidget</a>.</p><p>With this widget, you can define the format of datetime fields in your files with just a few lines of code so uploads will go smoothly and import with datetime matching your model.</p><p>The assumption here is that you have datetime fields in your django model, like this:</p><pre>user_created_at = models.DateTimeField(blank=True, null=True)</pre><p>In the same directory as your admin.py and models.py file, create a file called resources.py.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/222/1*mt1_IwYLoSKWAn81x3jkMA.png" /><figcaption>Add a file called resources.py to your app directory.</figcaption></figure><p>Inside that file you’ll create a django-import-export resource that helps handle the import formatting. Here’s what worked for my project.</p><pre>from import_export import resources<br>from import_export.fields import Field<br>from import_export.widgets import DateTimeWidget<br><br>from pages.models import Tweet<br><br>class TweetResource(resources.ModelResource):<br>   #reformat datetime fields from TwSearch .csv files to Djago datetime default.<br>   created_at = Field(attribute=&#39;created_at&#39;, column_name=&#39;created_at&#39;, widget=DateTimeWidget(&#39;%m/%d/%Y, %I:%M:%S %p&#39;))<br>   user_created_at = Field(attribute=&#39;user_created_at&#39;, column_name=&#39;user_created_at&#39;, widget=DateTimeWidget(&#39;%m/%d/%Y, %I:%M:%S %p&#39;))<br>   <br>   class Meta:<br>       model = Tweet</pre><p>In the above code, I imported the relevant import_export libraries, and the relevant model from my models.py file.</p><p>The TweetResource class handles the formatting. I put in the name of the field in the model and csv (the same in this case), use the DateTimeWidget, and define how the datetime field is formatted in the csv file I’m importing. (If you don’t need the time values you could use the DateWidget instead by importing and declaring that in your resource.)</p><p>Now that the resource has been created, you can import that to your admin.py file and reference it from your model.</p><pre>from import_export import resources<br>from import_export.admin import ImportExportModelAdmin<br>from .resources import TweetResource<br><br>class TweetAdmin(ImportExportModelAdmin):<br>    model = Tweet<br>    resource_class = TweetResource<br>...<br><br>admin.site.register(Tweet, TweetAdmin)</pre><p>That should do it. When setting up your ModelResource, be sure to format the datetime field to match what’s in your import file.</p><p>If you’re not interested in memorizing python datetime formatting, ask ChatGPT for some help:</p><pre>How would this datetime be formatted in python?<br>2024-03-19 22:23:47<br><br>In this example, the format string &quot;%Y-%m-%d %H:%M:%S&quot; specifies the desired format:<br><br>&quot;%Y&quot; represents the year with century as a decimal number.<br>&quot;%m&quot; represents the month as a zero-padded decimal number.<br>&quot;%d&quot; represents the day of the month as a zero-padded decimal number.<br>&quot;%H&quot; represents the hour (24-hour clock) as a zero-padded decimal number.<br>&quot;%M&quot; represents the minute as a zero-padded decimal number.<br>&quot;%S&quot; represents the second as a zero-padded decimal number.<br></pre><p>I hope this helps you get over a datetime hump and move your project forward.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e948e750e5c5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>