<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahsanuzzaman Khan</title>
    <description>The latest articles on DEV Community by Ahsanuzzaman Khan (@ahsankhan).</description>
    <link>https://dev.to/ahsankhan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F807358%2F44984dae-e93d-4b39-b4ab-4c6a5eeef959.png</url>
      <title>DEV Community: Ahsanuzzaman Khan</title>
      <link>https://dev.to/ahsankhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahsankhan"/>
    <language>en</language>
    <item>
      <title>AttributeError: 'Depends' object has no attribute 'get' FastAPI</title>
      <dc:creator>Ahsanuzzaman Khan</dc:creator>
      <pubDate>Fri, 31 Mar 2023 10:19:20 +0000</pubDate>
      <link>https://dev.to/ahsankhan/attributeerror-depends-object-has-no-attribute-get-fastapi-637</link>
      <guid>https://dev.to/ahsankhan/attributeerror-depends-object-has-no-attribute-get-fastapi-637</guid>
      <description>&lt;p&gt;While working with &lt;code&gt;FastAPI&lt;/code&gt; I've faced a problem about the above mention error. Though it is quite common but I didn't face it before.&lt;/p&gt;

&lt;p&gt;My use case is, I put a dependency injection which works perfectly fine within endpoint functions (the ones with &lt;code&gt;@app.post/@app.get&lt;/code&gt; decorators), but somehow doesn't work within plain functions.&lt;/p&gt;

&lt;p&gt;My code block has given below,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async def get_client():
    async with AsyncClient() as client:
        yield client
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async def get_third_party_result(client=Depends(get_client)):
    ...
    await client.get(url)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In summary what have I found denoted below. The actual link is &lt;a href="https://github.com/tiangolo/fastapi/issues/1693#issuecomment-665833384"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;you can't use Depends in your own functions, it has to be in FastAPI functions, mainly routes. You can, however, use Depends in your own functions when that function is also a dependency, so could can have a chain of functions.&lt;br&gt;
Eg, a route uses Depends to resolve a 'getcurrentuser', which also uses Depends to resolve 'getdb', and the whole chain will be resolved. But if you then call 'getcurrentuser' without using Depends, it won't be able to resolve 'getdb'.&lt;br&gt;
What I do is get the DB session from the route and then pass it down through every layer and function. I believe this is also better design.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For my purpose, anyway I need to test it explicitly. Thus to get rid of it, I prepare another method to get the result of actual function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async def hello():
   async with httpx.AsyncClient() as client:
     return await get_third_party_result(client=client)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And run it as,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import asyncio

asyncio.run(hello())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now I get proper result for further testing. &lt;br&gt;
&lt;strong&gt;Thanks for your reading.&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>fastapi</category>
    </item>
    <item>
      <title>How to add comments to model instances as generic relations</title>
      <dc:creator>Ahsanuzzaman Khan</dc:creator>
      <pubDate>Mon, 28 Feb 2022 07:52:05 +0000</pubDate>
      <link>https://dev.to/ahsankhan/how-to-add-comments-to-model-instances-as-generic-relations-2di4</link>
      <guid>https://dev.to/ahsankhan/how-to-add-comments-to-model-instances-as-generic-relations-2di4</guid>
      <description>&lt;p&gt;As a newbie of Django’s Generic Relation and Generic Foreign Key,I found that the documentation can be kind of confusing and hard to grab. Nevertheless, Generic Relations helped us a lot, and so I decided to write about it in this blog post :)&lt;/p&gt;

&lt;p&gt;When we have a foreign key, we are linking an instance of another model in the current model. So, we can access those very easily. Below code would work like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Author(models.Model):
    name = models.CharField(max_length=50)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=250)
    pages = models.IntegerField()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here you will have an instance of Author associated with all the information we have on the Book instance. That's cool because you can store several books, all linked to the same author.&lt;/p&gt;

&lt;p&gt;Imagine now that you want the same thing: associate an instance of a model with more information. But instead of having just an instance of Author (for example), you want the same information through several model classes (such as books and cds).&lt;/p&gt;

&lt;p&gt;GenericRelations is the saviour here. In a simple way we can say that GenericRelation is a foreign key that can store any instance of any of your models at any of your apps.&lt;/p&gt;

&lt;p&gt;Question can be arise here: Why is this useful? You can simply add more fields in your original model to accomplish your goal. Yes, this is true in most cases. However, for some specific cases, Generic Relations can be really handy. In our case, we needed the ability to add comments in two apps of our system. We needed the same functionality in different part of the system in a way it would be easily maintainable and ignore duplication: perfect time for Generic Relations.&lt;/p&gt;

&lt;p&gt;We started by creating an Comment model such as (Django 2.2):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models

class Comment(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.Charfield(max_length=50)
    content_object = GenericForeignKey('content_type', 'object_id')
    text = models.TextField(blank=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On this model the text field is a normal TextField from Django, used to store the comment itself. The other fields, content_type, object_id and content_objects are part of the Generic Relation we are adding here.&lt;/p&gt;

&lt;p&gt;Instances of ContentType represent and store information about the models installed on your project. Every time a new model is created, new instances of ContentTypes are automatically created. Here, the content_type will be a Foreign Key to the model you want to associate.&lt;/p&gt;

&lt;p&gt;The object_id, by the other end, is a simple &lt;code&gt;Charfield&lt;/code&gt; that will store an id of an object that is stored in your model. On the official Django documentation, you will find that the suggestion is to use PositiveIntegerField on this field. However, we use &lt;code&gt;uuid&lt;/code&gt; as our id fields so we had to change this to &lt;code&gt;Charfield&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You have the model, you have the id of the object you want to access so the content_objects will actually represent the instance of that particular object on that particular model. The GenericForeignKey does the magic for you!&lt;/p&gt;

&lt;p&gt;Let’s apply this model for something useful. Imagine that you have models for Books and CDs, and you want to to be able to add comments from your users in each book or cd available on your database.&lt;/p&gt;

&lt;p&gt;To create a new comment in a specific book all you need to do is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.contenttypes.models import ContentType
from .models import Book, Comment

book = Book.objects.first()
text = 'The message goes here'

new_comment = Comment(text=text,
                       content_object=book)
new_comment.save()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So you can first recover the instance you want to associate your comment with and send it as the content_object.&lt;/p&gt;

&lt;p&gt;You can do this with the Book model, the Cd model or any other model in any other app you have on your system. You won’t need to rewrite this comment to every app or every model you want to add a series of comments.&lt;/p&gt;

&lt;p&gt;Now you can ask: How can I recover the comments information in my Book or Cd instance? Here comes the easy part!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.contrib.contenttypes.fields import GenericRelation
from .models import Comment

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=250)
    pages = models.IntegerField()
    comments = GenericRelation(Comment)

class Cd(models.Model):
    artist = models.ForeignKey(Artists)
    title = models.CharField(max_length=250)
    comments = GenericRelation(Comment)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Done! You can add comments to any Book or Cd instance, are retrieve it by simply doing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;book.comments.all()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this would help you out. :)&lt;/p&gt;

</description>
      <category>django</category>
      <category>genericrelations</category>
    </item>
    <item>
      <title>How to activate `virtualenv` in Fish (Friendly interactive shell)?</title>
      <dc:creator>Ahsanuzzaman Khan</dc:creator>
      <pubDate>Tue, 08 Feb 2022 12:16:30 +0000</pubDate>
      <link>https://dev.to/ahsankhan/how-to-activate-virtualenv-in-fish-friendly-interactive-shell-10g0</link>
      <guid>https://dev.to/ahsankhan/how-to-activate-virtualenv-in-fish-friendly-interactive-shell-10g0</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;If you used to be a bash shell user and then decided to move to &lt;code&gt;fish&lt;/code&gt;, you will probably face a problem with activating your python virtual environment.&lt;/p&gt;

&lt;p&gt;The typical method to activate a &lt;code&gt;virtualenv&lt;/code&gt; is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ source your_env_name/bin/activate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This fails in Fish shell:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;source: Error while reading file “your_env_name/bin/activate”&lt;/code&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The solution to this problem was much a lot easier than googling it. Thankfully, &lt;code&gt;virtualenv&lt;/code&gt; ships with an activation script for specifically for Fish. Use that instead:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;$ source your_env_name/bin/activate.fish&lt;/code&gt;&lt;/p&gt;

</description>
      <category>fish</category>
    </item>
  </channel>
</rss>
